[
{
"name": "432",
"fc_type": 0,
"key": "0"
},
{
"sub_type": 0,
"fc_type": 0,
"key": "0"
},
{
"code": "432",
"fc_type": 0,
"key": "0"
},
{
"model": "432",
"fc_type": 0,
"key": "0"
},
{
"throughput": 423,
"fc_type": 0,
"key": "0"
},
{
"name": "432",
"fc_type": 0,
"key": "1"
},
{
"sub_type": 0,
"fc_type": 0,
"key": "1"
},
{
"code": "432",
"fc_type": 0,
"key": "1"
},
{
"model": "432",
"fc_type": 0,
"key": "1"
},
{
"throughput": 432,
"fc_type": 0,
"key": "1"
}
]
预期效果
[
{
"name": "432",
"sub_type": 0,
"code": "432",
"model": "432",
"throughput": 423,
"fc_type": 0,
"key": "0"
},
{
"name": "432",
"sub_type": 0,
"code": "432",
"model": "432",
"throughput": 423,
"fc_type": 0,
"key": "1"
},
]
用 reduce
方法
[
{
"name": "432",
"fc_type": 0,
"key": "0"
},
{
"sub_type": 0,
"fc_type": 0,
"key": "0"
},
{
"code": "432",
"fc_type": 0,
"key": "0"
},
{
"model": "432",
"fc_type": 0,
"key": "0"
},
{
"throughput": 423,
"fc_type": 0,
"key": "0"
},
{
"name": "432",
"fc_type": 0,
"key": "1"
},
{
"sub_type": 0,
"fc_type": 0,
"key": "1"
},
{
"code": "432",
"fc_type": 0,
"key": "1"
},
{
"model": "432",
"fc_type": 0,
"key": "1"
},
{
"throughput": 432,
"fc_type": 0,
"key": "1"
}
].reduce((prev, current) => {
const obj = prev.find(item => item.key === current.key)
if (obj) {
// 结果数组中已经有这个 key 了就只添加属性
Object.assign(obj, current)
} else {
prev.push(current)
}
return prev
}, [])