let costList = [{name: 'a',id: 1}, {name: 'a',id: 2}, {name: 'b',id: 3}, {name: 'c',id: 4},
{name: 'c',id: 6}, {name: 'b',id: 6}, {name: 'd',id: 7}];
1.reduce方法
根据数组对象的name属性作为条件去重
let Conditions = {};
costList = costList.reduce((preVal, curVal) => {
// console.log(incomeList,'2')
Conditions[curVal.name] ? '' : Conditions[curVal.name] = preVal.push(curVal)
console.log(preVal, '1')
return preVal
}, []);
2. es6的Map()
根据数组对象的name属性作为条件去重
let map = new Map();
for (let item of costList) {
if (!map.has(item.name)) {
map.set(item.name, item)
}
costList = [...map.values()]
};