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

133
Vistas
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 Respuestas
Responde la pregunta

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 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