数组对象去重处理(自定义根据哪个键名来去重)

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title>数组去重</title>
 5 </head>
 6 <body>
 7 <script type="text/javascript">
 8     var projectLineLis = [
 9     {value: 1, label: "雪亮标品", $$hashKey: "067"},
10     {value: 2, label: "大数据", $$hashKey: "068"},
11     {value: 3, label: "大联动", $$hashKey: "069"},
12     {value: 4, label: "全科采集", $$hashKey: "06A"},
13     {value: 5, label: "铁路护路", $$hashKey: "06B"},
14     {value: 6, label: "平安中国", $$hashKey: "06C"},
15     {value: 7, label: "社会化服务", $$hashKey: "06D"},
16     {value: 1, label: "雪亮标品", $$hashKey: "06E"},
17     {value: 8, label: "12", $$hashKey: "06F"},
18     {value: 8, label: "45", $$hashKey: "06G"},
19     {value: 1, label: "雪亮标品", $$hashKey: "06H"},
20     {value: 2, label: "大数据", $$hashKey: "06I"},
21     {value: 3, label: "大联动", $$hashKey: "06J"},
22 ]
23     function arrayUnique2(arr, name) {
24       var hash = {};
25       return arr.reduce(function (item, next) {
26         hash[next[name]] ? '' : hash[next[name]] = true && item.push(next);
27         return item;
28       }, []);
29     }
30     console.log(arrayUnique2(projectLineLis,'label'))
31 </script>
32 </body>
33 </html>

效果如下所示:

posted @ 2018-08-23 16:09  呀,西蓝花  阅读(470)  评论(0编辑  收藏  举报