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

218
Views
How can i merge json array objects using javaScript/TypeScript
input = [ { id: 1, name: "aaa" }, { id: 1, name: "bbb" }, { id: 2, name: "ccc" }, { id: 3, name: "ddd" } ]

Need output like below..

[{id: 1,name: "aaa, bbb"},{id: 2,name: "ccc"},{id: 3,name: "ddd"}]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can easily achieve the result using reduce

const input = [
  { id: 1, name: "aaa" },
  { id: 1, name: "bbb" },
  { id: 2, name: "ccc" },
  { id: 3, name: "ddd" },
];

const result = input.reduce((acc, curr) => {
  const objInAcc = acc.find((o) => o.id === curr.id);
  !objInAcc ? acc.push(curr) : (objInAcc.name += `, ${curr.name}`);
  return acc;
}, []);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You can group data from your array by id with a reducer like so

const input = [{ id: 1, name: "aaa" }, { id: 1, name: "bbb" }, { id: 2, name: "ccc" }, { id: 3, name: "ddd" }]

const groupedNames = input.reduce<Record<number,string[]>>((grouped, { id, name }) => (
    { ...grouped, [id]: (grouped[id] || []).concat(name) }
), {})

// then get the names as
groupedNames[id].join(', ')

// or map it to your preferred format
Object.entries(groupedNames).map(([id, names]) => ({ id, name: names.join(', ') }))
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!