数组去重,数组里的对象重复

let data = [
{ id: 201801, name: '张三', age: 15, },
{ id: 201804, name: 'John', age: 18, },
{ id: 201802, name: '李四', age: 18, },
{ id: 201801, name: '张三', age: 15, },
{ id: 201805, name: 'Jack', age: 18, },
{ id: 201803, name: '王五', age: 10, },
{ id: 201805, name: 'Jack', age: 18, },
{ id: 201804, name: 'John', age: 18, },
{ id: 201805, name: 'Jack', age: 18, },
];
let hash = {};
data = data.reduce(function(preVal, curVal) {
hash[curVal.id] ? ' ' : hash[curVal.id] = true && preVal.push(curVal);
return preVal
}, []);
console.log(333,data);

posted @ 2020-10-23 17:13  前端张大碗  阅读(356)  评论(0编辑  收藏  举报