其他分享
首页 > 其他分享> > 由对象组成的数组如何去重?

由对象组成的数组如何去重?

作者:互联网

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((preVal, curVal) => {
hash[curVal.name] ? '' : hash[curVal.name] = true && preVal.push(curVal);
return preVal
}, []);

console.log('1111', data, hash);

标签:name,对象,18,age,curVal,数组,data,id,组成
来源: https://www.cnblogs.com/shamszhang/p/11251461.html