I have below scenario where I am trying to merge two array of objects.
The current code works, but I am looking for a more efficient way.
var constList = [
{ name: "jack", id: "1", designation: "hr" },
{ name: "mary", id: "2", designation: "it" },
{ name: "john", id: "3", designation: "fin" }
]
var apiList = [
{ name: "jack", id: "1", height: "10" },
{ name: "mary", id: "2", height: "20" }
]
var temp = [];
constList.forEach(x => {
apiList.forEach(y => {
if (x.id === y.id) {
temp.push({ ...x,
...y
});
}
});
});
console.log(temp);
Only the matching elements from apiList should be merged with constList.
The output is correct, can anyone guide me with best way to to it.