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

234
Vistas
How to map or zip a simple array to an array of arrays in JS with vanilla JS

I have an array of food categories, and I want to pair each category to a list of product codes that are in an array of arrays.

const categories = [ "dairy", "fruit", "breakfast", "frozen" ];  
const codes = [['2340', '89878', '9998'], ['89878'], ['987979', '984', '29898', '299'], ['0008', '2988'}]

categories and codes both have 10 elements each. How can I end up with ...

const onSale = {dairy:['2340', '89878', '9998'], fruit: ['89878'], breakfast:['987979', '984', '29898', '299'] , frozen: ['0008', '2988'}

Im a newbie, so Vanilla JS preferred.

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

0

You can use the second parameter passed in to the map function to know which index this element is at. Then pull the matching index from the other array. Since you want a resulting object, use fromEntries to wrap up the array that results from map.

const categories = [ "dairy", "fruit", "breakfast", "frozen" ];  
const codes = [['2340', '89878', '9998'], ['89878'], ['987979', '984', '29898', '299'], ['0008', '2988']];

const onSale = Object.fromEntries(
  categories.map((cat, i) => [cat, codes[i]])
);

console.log(onSale)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do it by reduce method, like this:

const categories = [ "dairy", "fruit", "breakfast", "frozen" ];  
const codes = [['2340', '89878', '9998'], ['89878'], ['987979', '984', '29898', '299'], ['0008', '2988']]
const onSales = categories.reduce((acc, key, index)=> {
    acc[key] = codes[index];
    return acc;
}, {});
console.log(onSales)

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