2 回答
TA贡献1828条经验 获得超6个赞
使用,您可以对项目Array.prototype.reduce进行分组和合并。uid
const res1 = [
{
uid: 'RBZ7K2122715',
Model: 'Mod1',
lastReceived: 'date1'
},
{
uid: 'RBZ7K2123333',
Model: 'Mod2',
lastReceived: 'date2'
}
];
const res2 = [
{
uid: 'RBZ7K2122715',
Status: [ ['Object1'], ['Object2'], ['Object3'] ]
},
{
vin: 'RBZ7K2123333',
Status: [ ['Object4'], ['Object5'] ]
}
];
const groupBy = [...res1, ...res2].reduce((acc, cur) => {
acc[cur.uid] ? acc[cur.uid] = {
...acc[cur.uid], ...cur
} : acc[cur.uid] = cur;
return acc;
}, {});
const output = Object.values(groupBy);
console.log(output);
TA贡献1877条经验 获得超1个赞
res1
使用map循环,找到res2
具有相同 uid 的项目并返回新对象。
const result = res1.map(item => {
const index = res2.findIndex(item2 => item2.uid === item.uid)
return {
...item,
Status: index !== -1 ? res2[index].Status : []
}
});
添加回答
举报