let data = [
{id:1,name:'obj'},
{id:3,name:'string'},
{id:2,name:'arr'},
{id:1,name:'string'}
];
// 根据对象中的属性进行去重
function filterArr(arr, name) {
let hash = {};
return arr.reduce((ss, item) => {// reduce累计器, ss是具体满足条件后返回的数据, item是数组依次循环的每一项
hash[item[name]] ? '' : hash[item[name]] = true && ss.push(item);
// 1、判断对象的键值是否存在
return ss;
}, []);
}
let arr2 = filterArr(data, 'id');
console.log(arr2);
网友评论