Js array merge problem.

< H2 > I want to (gracefully) merge two arrays < / H2 >
let a = [{
    id: "1",
    name: "test1",
    count: 2
},{
    id: "2",
    name: "test2",
    count: 3
}]

let b = [{
    id: "1",
    name: "test1",
    count: 1
},{
    id: "3",
    name: "test3",
    count: 3
}]

// 
[{
    id: "1",
    name: "test1"
    count: 1
},{
    id: "2",
    name: "test2",
    count: 3
},{
    id: "3",
    name: "test3",
    count: 3
}]

override a different attributes according to id,b

Feb.27,2021


""

function update(dst, src) {
    for(let key in src) dst[key] = src[key];
}

// Create index
let index = {};
a.forEach((v, k) => index[v.id] = k);

b.forEach(v => {
  if(v.id in index) update(a[index[v.id]], v);
  else a.push(v);
});

in addition, reasoning is "gracefully"-sharp (funny).

Menu