接口获取的数据对象去重,根据id

数据:

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, },
];

reduce方法:

let hash = {};
data = data.reduce((preVal, curVal) => {
hash[curVal.id] ? ‘’ : hash[curVal.id] = true && preVal.push(curVal);
return preVal
}, [])

你可能感兴趣的:(接口获取的数据对象去重,根据id)