[{ method:'fly', code:'1', count:1,},{ method:'fly', code:'1', count:2,}][{ method:'fly', code:'1', count:3,//count相加了}]自己实现了一个,不知道有么有bug,求指点function mergeOrder(order) { return order.reduce((a, b) => { let flag = a.some((item, index) => { return item.method === b.method && item.code === b.code; }); if (flag) { for (let item of a) { if (item.method === b.method && item.code === b.code) { item.count += b.count; } } } else { a.push(b); } return a; }, [{ method: '', code: '', count: 0 }]);}
1 回答
aluckdog
TA贡献1847条经验 获得超7个赞
如下,result是你想要得到的数组:
var result = [];
var arr = [{
method:'fly',
code:'1',
count:1,
},{
method:'fly',
code:'1',
count:2,
}];
for(var i =0; i< arr.length; i++){
var isFind = false;
for(var j =0 ; j< result.length; j++){
if(arr[i].method === result[j].method && arr[i].code === result[j].code){
result[j].count += arr[i].count;
isFind = true;
break;
}
}
if(!isFind)
result.push(arr[i]);
}
添加回答
举报
0/150
提交
取消