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

76
Vistas
Filter array of objects with another array of objects and get new array of objects

I have two array of objects and I want to get a new one which is combination of those two.

const all_products = [ 
  {
   id : 1,
   name : "Coca Cola",
   price : 12.34
  },
 {
  id : 2,
  name : "Marbo Chips",
  price : 8.34
 },
 {
 id : 3,
 name : "Seven Days",
 price : 4.34
 },
{
id : 4,
name : "Lays",
price : 32.34
 },
{
id : 5,
name : "Pringles",
price : 2.34
}
 ];

And this one:

const shopping_cart = [
{
 id : 1,
 qty : 4
},
{
 id : 5,
 qty : 2
}
 ];

I want to filter through all_products array by comparing if id's are the same and want to add qty to that object and get this

new_arr = [
  {
  id: 1,
  name: "Coca Cola",
  price: 12.34,
  qty: 4
 }, {
  id: 5,
  name: "Pringles",
  price: 2.34,
  qty: 2
 }]

I've succeeded by using this

 const test = [];

  for( let one_product of all_products ) {
     for( let sc_item of shopping_cart ){
        if(one_product.id === sc_item.id){
          let it = {
                 ...one_product,
                 qty : sc_item.qty
         }
         test.push(it);
       }
      }
     }

But I want to do this by using js array functions like filter,some,reduce etc.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Hi,

This is your code

const all_products = [ 
  {
   id : 1,
   name : "Coca Cola",
   price : 12.34
  },
 {
  id : 2,
  name : "Marbo Chips",
  price : 8.34
 },
 {
 id : 3,
 name : "Seven Days",
 price : 4.34
 },
{
id : 4,
name : "Lays",
price : 32.34
 },
{
id : 5,
name : "Pringles",
price : 2.34
}
 ];
const shopping_cart = [
  {
   id : 1,
   qty : 4
  },
  {
   id : 5,
   qty : 2
  }
   ];
   
const test = [];
all_products.forEach(one_product => {
   shopping_cart.forEach(sc_item => {
      if(one_product.id === sc_item.id){
        let it = {
           ...one_product,
           qty : sc_item.qty
        }
        test.push(it);
      }
   })
})
console.log(test)

about 4 years ago · Juan Pablo Isaza Denunciar

0

There you go

const all_products = [{
    id: 1,
    name: "Coca Cola",
    price: 12.34
  },
  {
    id: 2,
    name: "Marbo Chips",
    price: 8.34
  },
  {
    id: 3,
    name: "Seven Days",
    price: 4.34
  },
  {
    id: 4,
    name: "Lays",
    price: 32.34
  },
  {
    id: 5,
    name: "Pringles",
    price: 2.34
  }
];
const shopping_cart = [{
    id: 1,
    qty: 4
  },
  {
    id: 5,
    qty: 2
  }
];

const updatedCart = shopping_cart.map((item) => {
  const moreProductInfo = all_products.filter((product) => product.id === item.id)[0];
  return {
    ...item,
    ...moreProductInfo
  }
})

console.log(updatedCart)

about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to keep the non matched entries:

 var merged = all_products.filter(function(a) {
  return shopping_cart.filter(function(b){
   if (a.id == b.id)
   {
   return Object.assign(a, b)
   }
  })
});
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