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

99
Vistas
Comparando dos matrices, tenga una propiedad de la primera a la segunda donde la identificación del elemento es la misma

Quiero trabajar con dos matrices comparándolas y fusionar algunas propiedades:

Tengo mi array1 y array2 como:

 array1 = [ { id: 10, name: 'abc', otherData: 'other' }, { id: 20, name: 'def', otherData: 'other' }, { id: 30, name: 'ghi', otherData: 'other' }, ]; array2 = [ { id: 10, nameV2: 'xyz', otherData: 'other' }, { id: 20, nameV2: 'pqr', otherData: 'other' }, { id: 30, nameV2: 'tvs', otherData: 'other' }, ];

Estoy esperando este resultado en el que compararé ambas matrices, repetiré los elementos, si el id es el mismo, entonces tenga la clave nameV2 de array2 en los elementos array1's

Rendimiento esperado :

 array1 = [ { id: 10, name: 'abc', otherData: 'other', nameV2: 'xyz' }, { id: 20, name: 'def', otherData: 'other', nameV2: 'pqr' }, { id: 30, name: 'ghi', otherData: 'other', nameV2: 'tvs' }, ];

¿Cómo lograr esto?

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

0

Aquí hay un enfoque para hacer esto, creamos un mapa de búsqueda ( idNameV2Map ) usando array2 y luego lo usamos para obtener el resultado deseado

 const array1 = [{ id: 10, name: 'abc', otherData: 'other' }, { id: 20, name: 'def', otherData: 'other' }, { id: 30, name: 'ghi', otherData: 'other' }]; const array2 = [{ id: 10, nameV2: 'xyz', otherData: 'other' }, { id: 20, nameV2: 'pqr', otherData: 'other' },{ id: 30, nameV2: 'tvs', otherData: 'other' }]; const idNameV2Map = array2.reduce((acc, curr) => { acc[curr.id] = curr.nameV2; return acc; }, {}); const output = array1.map((item) => { if (idNameV2Map[item.id]) { return { ...item, nameV2: idNameV2Map[item.id] }; } return item; }); console.log(output);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Otro enfoque usando Array.find()
con agregar condicionalmente la propiedad nameV2 al objeto si existe.

 const array1 = [{ id: 10, name: 'abc', otherData: 'other' }, { id: 20, name: 'def', otherData: 'other' }, { id: 30, name: 'ghi', otherData: 'other' }]; const array2 = [{ id: 10, nameV2: 'xyz', otherData: 'other' }, { id: 20, nameV2: 'pqr', otherData: 'other' },{ id: 30, nameV2: 'tvs', otherData: 'other' }]; const output = array1.map((item) => { let objFound = array2.find(obj=>obj.id===item.id); return { ...item, ...(objFound && {nameV2: objFound.nameV2}) }; }); console.log(output);

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