首页 > 编程语言 > 详细

数组对象合并,值累加

时间:2019-11-21 16:12:33      阅读:131      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 1:方案1:  

let result = []
data.reduce((result, item) => {
const { source, target, value } = item;
const tmp = result.get(`${source}|${target}`) || {};
result.set(`${source}|${target}`, { ...tmp, value: value + (tmp.value || 0) });
return result
}, new Map()).forEach((value, key) => {
[source, target] = key.split( "|" )
result.push( { source,target, value } )
})
2:方案二
// let newArr = []
// data.forEach(el => {
// const result = newArr.findIndex(ol => { return el.source === ol.source && el.target === ol.target })
// if (result !== -1) {
// newArr[result].value = newArr[result].value + el.value
// } else {
// newArr.push(el)
// }
// })

数组对象合并,值累加

原文:https://www.cnblogs.com/binglove/p/11905848.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!