js合并数组对象中的相同项,并统计数量和
let list=[
{
id:11,
name:'apple',
num:2
},
{
id:11,
name:'apple',
num:3
},
{
id:22,
name:'pig',
num:2
},
{
id:11,
name:'apple',
num:2
},
]
list = duce((obj, item) => {
let find = obj.find(i => i.id === item.id)
let _d = {
...item,
frequency: 1
}
find ? (find.num+=item.num,find.frequency++ ): obj.push(_d)
js合并两个数组return obj
}, [])
输出=>
[
{
id: 11,
name: "apple",
frequency:3,
num: 7
},{
id: 22,
name: "pig",
frequency:1,
num: 2,
}
]

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。