Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda