JavaScript使用reduce分组及合并数组的object对象
对象数组示例:
var objects = [
{name:'group1', usedCount: 2, color:'red'},
{name:'group1', usedCount: 1, color:'blue'},
{name:'group1', usedCount: 1, color:'orange'},
{name:'group2', usedCount: 2, color:'blue'},
{name:'group2', usedCount: 2, color:'red'},
{name:'group3', usedCount: 1, color:'red'},
{name:'group3', usedCount: 4, color:'red'}
]
分组合并后的结果
[
{name:'group1', usedCount: 4, color:['red','blue','orange']},
{name:'group2', usedCount: 4, color:['blue','red']},
{name:'group3', usedCount: 5, color:['red']}
]
这是根据对象的name进行合并,合并后,usedCount累加,color合并为数组。
reduce方法
var result= objects.reduce((groups, item)=>{
var groupFound= groups.find(arrItem => item.name === arrItem.name);
if(groupFound) {
groupFound.usedCount += item.usedCount;
if(groupFound.color.indexOf(item.color) == -1) { //去重
groupFound.color.push(item.color);
}
} else {
//不要直接在原来的对象修改,新建对象
var newGroup = {
name: item.name,
usedCount: item.usedCount,
color: [item.color]
}
groups.push(newGroup);
}
return groups;
},[]);
console.log(result);