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

60
Views
Merging array of objects by different ids

I have such a case:

const ids = {
// id: parentId
  "2": "1",
  "3": "1"
}

const input = [
  {
    id: "1",
    data: [1],
  },
   {
     id: "2",
     data: [2]
   },
   {
     id: "3",
     data: [3]
   },
   {
     id: "4",
     data: [4]
   }
]

const output = [
  {
    id: "1",
    data: [1,2,3]
  },
  {
    id: "4",
    data: [4]
  }
]

With this ids I wanted to create some kind of config, which will determine which object in input array should have merged data (if there is no id-parentId pair defined, it should stay as it is). I belive code above is better explanation than this story. I tried with some mappings but without any nice result. This 'ids' array is an example, maybe there is a better way to define it so it will simplify the case? What do you think? I will be grateful for any hint

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use reduce(). Create the output first as an object that uses id as the key. You can convert it to an array at the end with Object.values().

const ids = {
  // id: parentId
  "2": "1",
  "3": "1"
}

const input = [{
    id: "1",
    data: [1],
  },
  {
    id: "2",
    data: [2]
  },
  {
    id: "3",
    data: [3]
  },
  {
    id: "4",
    data: [4]
  }
]

const output = Object.values(input.reduce((acc, {
  id,
  data
}) => {
  if (id in ids) { // replace id with parent
    id = ids[id];
  }
  if (id in acc) {
    acc[id].data = acc[id].data.concat(data);
  } else {
    acc[id] = {
      id,
      data};
  }
  return acc;
}, {}));

console.log(output);

about 4 years ago · Juan Pablo Isaza Report

0

You could take the references for same groups.

const
    ids = { 2: "1", 3: "1" },
    input = [{ id: "1", data: [1] }, { id: "2", data: [2] }, { id: "3", data: [3] }, { id: "4", data: [4] }],
    output = Object.values(input.reduce((r, { id, data }) => {
        id = ids[id] ?? id;
        (r[id] ??= { id, data: [] }).data.push(...data);
        return r;
    }, {}));

console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

You can try to cycle the input and modifying it in place, something like this should work:

const input = [
  {
    id: '1',
    data: [1]
  },
  {
    id: '2',
    data: [2]
  },
  {
    id: '3',
    data: [3]
  },
  {
    id: '4',
    data: [4]
  }
]

const config = {
  2: '1',
  3: '1'
}

const merge = ({ input, config } = {}) => {
  for (const [childId, parentId] of Object.entries(config)) {
    const childIndex = input.findIndex(i => i.id == childId)
    const parent = input.find(i => i.id == parentId)
    parent.data.push(...input[childIndex].data)
    input.splice(childIndex, 1)
  }

  return input
}

console.log(merge({ input, config }))

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!