let arr = [
{"渠道":"河南","金额":0},
{"渠道":"河南","金额":10},
{"渠道":"河南","金额":20},
{"渠道":"河北","金额":0},
{"渠道":"河北","金额":10},
{"渠道":"河北","金额":10},
]
let idArr = [];
let resultArr = []
for(let i = 0;i < arr.length; i++){
let index = idArr.indexOf(arr[i]['渠道'])
if(index > -1){
// 说明这个渠道有重复的value 直接塞数据
resultArr[index]['金额'] = resultArr[index]['金额'] + ','+arr[i]['金额']
}else{
idArr.push(arr[i]['渠道'])
resultArr.push(arr[i])
}
}
resultArr.map((x,i)=>{
resultArr[i]['金额'] = x['金额'].split(',')
for(let k in resultArr[i]['金额']){
resultArr[i]['金额'][k] = Number(resultArr[i]['金额'][k])
}
})
console.log('idarr--->',idArr);
console.log(resultArr);
数组中重复元素去重并合并
最新推荐文章于 2023-11-22 10:17:46 发布