当 name 和 type 同时相同的情况下 value 相加合并
[
{
name:'A',
value:10,
type:'Z'
},
{
name:'A',
value:5,
type:'Z'
},
{
name:'B',
value:3,
type:'S'
},
{
name:'B',
value:2,
type:'S'
},
{
name:'C',
value:10,
type:'Z'
},
{
name:'C',
value:12,
type:'E'
}
]
期望结果
[
{
name:'A',
value:15,
type:'Z'
},
{
name:'B',
value:5,
type:'S'
},
{
name:'C',
value:10,
type:'Z'
},
{
name:'C',
value:12,
type:'E'
},
]
用
reduce
解决吧经提醒,已修复改变原数组bug