Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
How do I combine two arrays in react to get a new one with all of the items from the previous two?

I am trying to combine two arrays based on a shared property they both have. How can I do this in react? I want to combine them to create one array that contains the checkbox as well as all of the other items.

Here are two sample arrays:

const array1 = [ 
  {Handle: "handle1", title: "handle1"},
  {Handle: "handle2", title: "handle2"},
  {Handle: "handle3", title: "handle3"} ]
const array2 = [ 
  {Handle: "handle1", checkbox: true},
  {Handle: "handle2", checkbox: false},
  {Handle: "handle3", checkbox: true} ]

Result:

const array2 = [ 
  {Handle: "handle1", checkbox: true, title:"handle1"},
  {Handle: "handle2", checkbox: false, title:"handle2"},
  {Handle: "handle3", checkbox: true, title:"handle3"} ]

How do I combine them in such a way that I get a new array that contains the handle, title and checkbox all in the right places?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Something like this may work, not sure how much the data will change with the object name you want to combine.

const array1 = [ 
  {Handle: "handle1", title: "handle1"},
 {Handle: "handle2", title: "handle2"},
 {Handle: "handle3", title: "handle3"} ]
 
 const array2 = [ 
  {Handle: "handle1", checkbox: true},
 {Handle: "handle2", checkbox: false},
 {Handle: "handle3", checkbox: true} ]
 
 
 const newArr = array1.map(v => {
    let obj = array2.find(o => o.Handle === v.Handle)
  
  if(obj) {
    v.checkbox = obj.checkbox
  }
  return v
 })
 
 console.log(newArr)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can like so:

const array1 = [ 
  {Handle: "handle1", title: "handle1"},
 {Handle: "handle2", title: "handle2"},
 {Handle: "handle3", title: "handle3"} ]

const array2 = [ 
  {Handle: "handle1", checkbox: true},
 {Handle: "handle2", checkbox: false},
 {Handle: "handle3", checkbox: true} ]

const array3 = array1.map(({ Handle, title }, idx) => ({Handle, title, checkbox: array2[idx].checkbox}));
console.log(array3)

const array1 = [ 
  {Handle: "handle1", title: "handle1"},
 {Handle: "handle2", title: "handle2"},
 {Handle: "handle3", title: "handle3"} ]

const array2 = [ 
  {Handle: "handle1", checkbox: true},
 {Handle: "handle2", checkbox: false},
 {Handle: "handle3", checkbox: true} ]

const array3 = array1.map(({ Handle, title }, idx) => ({Handle, title, checkbox: array2[idx].checkbox}));
console.log(array3)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here we are the using map method and Object.assign method to merge the array of objects by using id.

const array1 = [ 
  {Handle: "handle1", title: "handle1"},
 {Handle: "handle2", title: "handle2"},
 {Handle: "handle3", title: "handle3"} ]
const array2 = [ 
  {Handle: "handle1", checkbox: true},
 {Handle: "handle2", checkbox: false},
 {Handle: "handle3", checkbox: true} ]
 

  console.log(array1.map((item1,i)=>{
     if(array2.find(item2 => item2.Handle === item1.Handle)){
       return Object.assign({},item1,array2.find(item2 => item2.Handle === item1.Handle))
     }
  }))

And you can do like this.

const array1 = [ 
  {Handle: "handle1", title: "handle1"},
 {Handle: "handle2", title: "handle2"},
 {Handle: "handle3", title: "handle3"} ]
const array2 = [ 
  {Handle: "handle1", checkbox: true},
 {Handle: "handle2", checkbox: false},
 {Handle: "handle3", checkbox: true} ]
 
 let mergedarray = array1.map(itm => ({
        ...array2.find((item) => (item.id === itm.id) && item),
        ...itm
 }));
 console.log(mergedarray)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda