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

318
Vistas
¿Cómo comparo y combino dos matrices de objetos en una sola?

He estado atrapado tratando de implementar esto para siempre. ¿Crees que podrías ayudarme a fusionar dos objetos de matriz basados en la clave PartitionKey y si PartitionKey no coincide, aún así agregarlo a la matriz de todos modos?

 let arr1 =[ { PartitionKey: '047', AttributionCount: 61 }, { PartitionKey: '043', AttributionCount: 136 }, { PartitionKey: '053', AttributionCount: 36 } ]; let arr2 = [ { PartitionKey: '043', ReportCount: 5 }, { PartitionKey: '047', ReportCount: 2 }, { PartitionKey: '045', ReportCount: 2 }, { PartitionKey: '041', ReportCount: 3 } ] let arr3 = arr1.map((item, i) => Object.assign({}, item, arr2[i])); console.log(arr3);

Me gustaría devolver el resultado de

 [ { PartitionKey: '047', ReportCount: 2, AttributionCount: 61 }, { PartitionKey: '043', ReportCount: 5, AttributionCount: 136 }, { PartitionKey: '053', ReportCount: 0, AttributionCount: 36 }, { PartitionKey: '045', ReportCount: 2, AttributionCount: 0}, { PartitionKey: '041', ReportCount: 3, AttributionCount: 0} ]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Puedes hacerlo así:

 let arr1 =[ { PartitionKey: '047', AttributionCount: 61 }, { PartitionKey: '043', AttributionCount: 136 }, { PartitionKey: '053', AttributionCount: 36 } ]; let arr2 = [ { PartitionKey: '043', ReportCount: 5 }, { PartitionKey: '047', ReportCount: 2 }, { PartitionKey: '045', ReportCount: 2 }, { PartitionKey: '041', ReportCount: 3 } ] let arr3 = arr2.map((item, i) => { const itemArray1 = arr1.find(x => x.PartitionKey == item.PartitionKey) || {AttributionCount: 0} return {...item, ...itemArray1} }); console.log('arr3', arr3);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Intente usar el método object.assign() para este problema.

por ejemplo: const returnedTarget = Object.assign(target, source);

referencia: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

about 4 years ago · Juan Pablo Isaza Denunciar

0

mi manera...

 let arr1 = [ { PartitionKey: '047', AttributionCount: 61 } , { PartitionKey: '043', AttributionCount: 136 } , { PartitionKey: '053', AttributionCount: 36 } ] let arr2 = [ { PartitionKey: '043', ReportCount: 5 } , { PartitionKey: '047', ReportCount: 2 } , { PartitionKey: '045', ReportCount: 2 } , { PartitionKey: '041', ReportCount: 3 } ] const result = Object.entries( arr1 .concat(arr2) .reduce((res,{PartitionKey,...info}) => { if (!res[PartitionKey]) res[PartitionKey] = info else Object.assign(res[PartitionKey], info ) return res } ,{}) ).map( ([k,v]) => ({PartitionKey:k, AttributionCount:0, ReportCount:0, ...v})) console.log( result )
 .as-console-wrapper {max-height: 100%!important;top:0 }

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