Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!