const arr = [
{ id: 1, count: 2 },
{ id: 2, count: 1 },
{ id: 1, count: 3 },
{ id: 3, count: 4 },
{ id: 2, count: 2 }
];
const filteredArr = arr.reduce((accumulator, current) => {
const existingItem = accumulator.find(item => item.id === current.id);
if (existingItem) {
existingItem.count += current.count;
} else {
accumulator.push(current);
}
return accumulator;
}, []);
console.log(filteredArr);
数组对象根据某个值过滤重复的数据,并累加某个值
最新推荐文章于 2024-09-07 14:37:16 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)