Js array merging problem

var a = [
  {
    id: 1,
    name: "..."  
  },
  {
    id: 2,
    name: "..."
  }
]
var b = [
  {
    id: 1,
    age: 10
  },
  {
    id: 2,
    age: 10
  }
]

for example, with the above two arrays, I hope to get the following results:

[{ id: 1, name: "...", age: 10 }, { id: 2, name: "...", age: 10 }]

is there any more efficient way? There is no guarantee that the order of id in an and b is the same

Mar.04,2021

a.map( itema => Object.assign({}, itema, b.find( itemb => itemb.id === itema.id)) )

you can write this directly

var arr1 = ['a', 'b'];
var arr2 = ['c'];
var arr3 = ['d', 'e'];
[...arr1, ...arr2, ...arr3]
// [ 'a', 'b', 'c', 'd', 'e' ]

clipboard.png


I will write this

let map = {}
a.forEach(item => {
  map[item.id] = item
})
b.map(item => {
  let currentItem = map[item.id] || {}
  return { ...item, ...currentItem }
})
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-1e4c58b-43459.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-1e4c58b-43459.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?