數組對象去重處理(自定義根據哪一個鍵名來去重)

 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>

效果以下所示:javascript

相關文章
相關標籤/搜索