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

120
Visualizações
Replace an object that is inside of array if object id value from another array match

I have this arrays with objects that looks like this:

array1 = [
 0:{id:145, value:130000},
 1:{id:146, value:103300},
 2:{id:147, value:79500},
]

array2 = [
 0:{id:145, value:135000}
]

And I want to replace the object inside the array if the id of the object in array2 match with some id of the object in array1

So I expect something like this:

array1 = [
 0:{id:145, value:135000},
 1:{id:146, value:103300},
 2:{id:147, value:79500},
]

I have this code

array1.splice(1, 1, array2[0])

but it returns me this:

array1 = [
 0:{id:145, value:135000},
 1:{id:145, value:130000},
 2:{id:146, value:103300},
 3:{id:147, value:79500},
]

Any help I'll appreciate

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

array2.forEach(i1 => {
    const index = array1.findIndex(i2 => i2.id == i1.id);
    if(index > -1) {
        array1.splice(index, 1, i1);
  }
});
about 4 years ago · Juan Pablo Isaza Relatório

0

let array1 = [
 {id:145, value:130000},
 {id:146, value:103300},
 {id:147, value:79500},
]

let array2 = [
 {id:145, value:135000},
 {id:147, value:135023}
]
    array2.map(x => {
    let index = array1.findIndex(d=> d.id === x.id)
  array1[index] = x  
})
console.log(array1)

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