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

137
Visualizações
Compare Javascript Object field with another Object field

I have two Objects one of them has the Store Name and the other object has the Price for an item along with the Store ID on both objects. Such as;

obj1 = [
    {id: 1,name: "Store1"},
    {id: 2,name: "Store2"},
    {id: 3,name: "Store3"}
  ];

obj2= [
{ id: 1, price: 100 },
{ id: 2, price: 200 },
{ id: 3, price: 300 }
];

What I want to achieve is that compare obj1 id with obj2 id if they are the same get the price and the store name from the same id. What is the best way to achieve this? I have been trying to use Array.map or filter but can't really make it work. Thank you!

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

0

You can use map & find

const obj1 = [{
    id: 1,
    name: "Store1"
  },
  {
    id: 2,
    name: "Store2"
  },
  {
    id: 3,
    name: "Store3"
  },
  {
    id: 4,
    name: "Store3"
  }
];

const obj2 = [{
    id: 1,
    price: 100
  },
  {
    id: 2,
    price: 200
  },
  {
    id: 3,
    price: 300
  }
];

const newData = obj1.map((item, index) => {
  return {
    ...item,
    // if the item exist in obj2 then get the price else assign empty string
    price: obj2.find(elem => elem.id === item.id) ? .price || ''

  }


});
console.log(newData)

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