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

122
Vistas
¿Cómo mapear las claves de dos matrices de la misma longitud entre sí?

Quiero asignar la clave de una matriz a otra matriz anidada. Ambas matrices tienen la misma longitud.

Antes:

 const idListToInsert = [{ idToInsert: 1 }, { idToInsert: 3 }, { idToInsert: 4 }]; const otherObjectList = [ [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }], [{ id: 5 }, { id: 6 }, { id: 7 }], [{ id: 8 }, { id: 9 }], ];

Objetivo:

 const targetList = [ [ { id: 1, idToInsert: 1 }, { id: 2, idToInsert: 1 }, { id: 3, idToInsert: 1 }, { id: 4, idToInsert: 1 }, ], [ { id: 5, idToInsert: 3 }, { id: 6, idToInsert: 3 }, { id: 7, idToInsert: 3 }, ], [ { id: 8, idToInsert: 4 }, { id: 9, idToInsert: 4 }, ], ];

Me está costando un poco encontrar el punto de partida de este problema, especialmente debido a la estructura anidada de la segunda matriz.

Estoy agradecido por cada pista.

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

0

este es el código más claro que pude encontrar ...

 const idListToInsert = [{ idToInsert: 1 }, { idToInsert: 3 }, { idToInsert: 4 }]; const otherObjectList = [ [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }], [{ id: 5 }, { id: 6 }, { id: 7 }], [{ id: 8 }, { id: 9 }], ]; const result = otherObjectList.map((item, index) => item.map((value) => ({ ...value, ...idListToInsert[index], }))); console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Puede mapear la matriz externa otherObjectList y sus matrices internas y crear un nuevo objeto con propiedades adicionales.

 const idListToInsert = [{ idToInsert: 1 }, { idToInsert: 3 }, { idToInsert: 4 }], otherObjectList = [[{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }], [{ id: 5 }, { id: 6 }, { id: 7 }], [{ id: 8 }, { id: 9 }]], result = otherObjectList.map((a, i) => a.map(o => ({ ...o, ...idListToInsert[i] }))); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

const targetList = [] otherObjectList.forEach((subarray, index) => { const temp = [] subarray.forEach(id => { temp.push({id, idListToInsert[index]}) }) targetList.push(temp) })

No está probado, pero debería funcionar.

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