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

194
Vistas
How to merge objects of arrays? mixed key
  [
        {
            "_id": "6205576aaa478f466cca3809",
            "age": 51,
            "createdAt": "2022-02-10T18:20:26.451Z",
        },
        {
            "score": [
                0,
                1,
                3,
                1,
                0,
                2
            ],
            "_id": "62081d1bc83c452ef8e34e47",
            "key_findings": "Lorem ipsum",
            "subject": "6205576aaa478f466cca3809",
            "createdAt": "2022-02-12T20:48:27.574Z",
        }
    ]

What I want it to look like:

[
  {
    _id: 6205576aaa478f466cca3809,
    age: 51,
    createdAt: 2022-02-10T18:20:26.451Z,
    score: [ 0, 1, 3, 1, 0, 2 ],
    _id: 62081d1bc83c452ef8e34e47,
    description: 'XR CHEST AP PORTABLE',
    key_findings: 'Lorem impusm',
    subject: 6205576aaa478f466cca3809,
    createdAt: 2022-02-12T20:48:27.574Z,
  }
]

I tried everything I can. There are many examples on the internet but that works only if the object's keys are the same.

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

0

You would use object spread in the iteration of the array:

let merged={}
arr.map(item=> merged={...merged,...item})

"arr" is your array and merged is your expected answer

about 4 years ago · Juan Pablo Isaza Denunciar

0

As there are two properties _id they will only be one property which will remain. You can not have multiple key inside of an object with the same name.

Here is the code to merge all object as a single object you can take advantage of Array.prototype.reduce

let data = [
    {
        "_id": "6205576aaa478f466cca3809",
        "age": 51,
        "createdAt": "2022-02-10T18:20:26.451Z",
    },
    {
        "score": [
            0,
            1,
            3,
            1,
            0,
            2
        ],
        "_id": "62081d1bc83c452ef8e34e47",
        "key_findings": "Lorem ipsum",
        "subject": "6205576aaa478f466cca3809",
        "createdAt": "2022-02-12T20:48:27.574Z",
    }
];

const result = data.reduce((accumulator, item) => {
  return [{...accumulator[0], ...item}];
}, []);

console.log(result);

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