【js两个数组对象根据属性相同的值合并】
js两个数组对象根据属性相同的值合并
·
数组对象
const a=[{
uid: 1,
name: '黄鹤楼'
},{
uid: 2,
name: '鹳雀楼'
},{
uid: 3,
name: '岳阳楼'
},{
uid: 4,
name: '滕王阁'
}]
const b=[{
uid: 1,
address: '湖北武汉黄鹤楼'
},{
uid: 2,
address: '山西运城鹳雀楼'
},{
uid: 3,
address: '湖南岳阳岳阳楼'
},{
uid: 4,
address: '江西南昌滕王阁'
}]
// 期待的效果
const c=[{
uid: 1,
name: '黄鹤楼',
address: '湖北武汉黄鹤楼',
},{
uid: 2,
name: '鹳雀楼',
address: '山西运城鹳雀楼',
},{
uid: 2,
name: '岳阳楼',
address: '湖南岳阳岳阳楼',
},{
uid: 2,
name: '滕王阁',
address: '江西南昌滕王阁',
}]
合并方法
a.map(el => {
el.address = b.find(item => item.uid == el.uid).address
})
console.log("变更后a:",a)
更多推荐
已为社区贡献2条内容
所有评论(0)