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

188
Vistas
Typescript map collection

I have this complex models which i am trying to map.

  • I have an array of Carts containing id, name and etc
  • I have a dictionary where key is a type and its value is the different products.

I have added an example of what the result should looks like at the bottom of page.

My attempt but got stuck on how to filter

carts.forEach(cart => {
    const productList = cart.products; //list of products

    Object.entries(content.data).forEach(([key, values]) => {
      const sizeList = values.map(x => x.sizeList); //list of size
      // i am stuck here
    });
  });
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

const getCartsWithProductNames = (carts = [], content = {}) => {
  // create a Map with productId-sizeId as key, and size name as value
  const productSizeNameMap = 
    Object.values(content)
    .flat()
    .reduce((map, { productId, sizeList = [] }) => {
      sizeList.forEach(({ sizeId, name }) =>
        map.set(`${productId}-${sizeId}`, name)
      );
      return map;
    }, new Map);
  // return carts list with product items having names from productSizeNameMap
  return carts.map(({ cartId, cartName, products = [] }) => ({
    id: cartId,
    name: cartName,
    productItems: products.map(({ id, size, quantity, isVisible }) => ({
      id, quantity, isVisible, name: productSizeNameMap.get(`${id}-${size}`)
    }))
  }));
}

const 
  carts = [
    {
      cartId: 500,
      cartName: "Some name",
      products: [ { id: 1, size: 10, quantity: 1, isVisible: true }, { id: 2, size: 13, quantity: 10, isVisible: true } ]
    }
  ],
  content = {
    "Drinks": [
      { productId: 1, sizeList: [ { sizeId: 10, name: "100ml beer" }, { sizeId: 9, name: "200ml beer" } ]
      }
    ],
    "Food": [
      {
        productId: 2,
        sizeList: [ { sizeId: 12, name: "6 wings" }, { sizeId: 13, name: "12 wings" } ]
      }
    ]
};

console.log( getCartsWithProductNames(carts, content) );

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