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).

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea6e4e-46b54.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea6e4e-46b54.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?