[{
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
}]);
}
aluckdog
相关分类