1 回答

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以將值分組到一個(gè)數(shù)組中
const arr = [{
"0156": {
"test": "hi",
"test2": "abc"
}
},
{
"0156": {
"test": "hi2",
"test2": "abc1"
},
"0157": {
"test": "y1"
}
},
{
"0156": {
"test": "hi3"
},
"0158": {
"test": "ti2"
}
},
{
"0156": {
"test": "hi4"
},
"0157": {
"test": "y"
}
},
{
"0158": {
"test": "ti"
}
}
]
const res = arr.reduce(function(acc, curr) {
for (let p in curr) {
acc[p] = acc[p] || curr[p]
for (let p1 in curr[p])
acc[p][p1] = acc[p][p1] != curr[p][p1] ? [].concat(acc[p][p1], curr[p][p1]) : curr[p][p1]
}
return acc
}, {})
console.log(res)
添加回答
舉報(bào)