js数组对象的相同值相加合并
2021-01-27 22:14
标签:class length else 相加 filter 联网 blog merge span // js数组对象相同值相加,不同添加 来自互联网参考:https://blog.csdn.net/namechenfl/article/details/93750462 js数组对象的相同值相加合并 标签:class length else 相加 filter 联网 blog merge span 原文地址:https://www.cnblogs.com/jiaqi1719/p/12842196.htmlmergeArr(arr){
var newArr=[];
arr.forEach(item => {
var dataItem = item;
if(newArr.length>0) {
var filterValue = newArr.filter(v => {
return v.partNo == dataItem.partNo
})
if(filterValue.length>0){
newArr.forEach(n => {
if( n.partNo == filterValue[0].partNo){
n.itemQty = filterValue[0].itemQty + dataItem.itemQty
}
})
}else{
newArr.push(dataItem)
}
}else{
newArr.push(dataItem)
}
})
return newArr
}