js数组中重复的对象去重
https://www.cnblogs.com/shiyunfront/p/7423977.html
var arr = [
{
"value": 1,
"label": "小明"
},
{
"value": 2,
"label": "小红"
},
{
"value": 1,
"label": "小明"
},
{
"value": 3,
"label": "小花"
}
];
var hash = {};
arr = arr.reduce(function(item, next) {
hash[next.value] ? '' : hash[next.value] = true && item.push(next);
return item
}, [])
console.log(arr);
打印结果:
注意:
hash[next.value] ? '' : hash[next.value] = true && item.push(next);
上述代码中 value为去重的键名key