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

150
Vistas
Merge nested array of objects by comparing key
  • I have an 3 nested array of Objects and I need to merge them all in single array of objects by comparing key. and if any object have same key then their inner object will merge. And if key is not same then also the nested array of objects will merge*
var obj1 = {    
    a: {
    c: 3
  }
}

var obj2 = {    
    b: {
    e: 40
  }
}

var obj3 = {    
    d: {
    x: 30
  },
  a: {
    f: 66
  }
}

// The expected output will be like this - 

// 
/*OUTPUT::: 
{
    a: {
    c: 3,
    f: 66
  },
  b: {
    e: 40
  },
  d: {
    x: 30
  }
}

Please tell me which approach would be suitable ?
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can group the objects using Array.prototype.reduce.

const 
  obj1 = { a: { c: 3 } },
  obj2 = { b: { e: 40 } },
  obj3 = { d: { x: 30 }, a: { f: 66 } };

const res = [obj1, obj2, obj3].reduce((r, o) => {
  Object.entries(o).forEach(([k, v]) => {
    r[k] = { ...r[k], ...v };
  });
  return r;
}, {});

console.log(res);

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