js 数组排序 重复项相加返回新数组
2022-04-14 本文已影响0人
妖刀_
function mergeArr(todos, key) {
var newArr = [];
todos.forEach(item => {
var dataItem = item
if (newArr.length > 0) {
var filterValue = newArr.filter(v => {
return v.index == dataItem.index
})
if (filterValue.length > 0) {
newArr.forEach(n => {
if (n.index == filterValue[0].index) {
n[key]= filterValue[0][key]+ dataItem[key]
}
})
} else {
newArr.push(dataItem)
}
} else {
newArr.push(dataItem)
}
})
return newArr
}