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

196
Visualizações
Compare two arrays of objects; If second object contains the same key/value as the first return the second's, if not add key/value from the first

Consider the following: We have two arrays, each with an object.

let users: [
  {
    "id": "some#"
    "name": "some name",
    "data": "somedata"
  }
],


let products: [
    {
      "product": "some#"
      "name": "abc",
    },
]

Essentially compare both objects, if the second object has the same property as the first, keep the second prop and value if not add the prop/value from the first.

So the output is :

{
  "id": "some#",
  "name": "abc",
  "data": "somedata"
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try this:

if (!(users[0].name === products[0].name)) {
product[0].data = users[0].data
}
about 4 years ago · Juan Pablo Isaza Relatório

0

With the code below, for the data you have given, you can have that output you are looking for:

let users = [
  {
    id: "some#",
    name: "some name",
    data: "somedata"
  }
];

let products = [
  {
    product: "some#",
    name: "abc"
  }
];

for (key in users[0]) {
  if (products[0][key]) {
    users[0][key] = products[0][key];
  } else {
    continue;
  }
}
console.log(users);

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