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

394
Views
collect all objects with same id in one index of array inside array of objects(do it for all common) and make a new array of objects in JavaScript

I have an array of objects, something like this:

myArray = [
    { id: 1, name: 'sara' },
    { id: 2, name: 'json' },
    { id: 3, name: 'elin' },
    { id: 1, name: 'linda' },
    { id: 2, name: 'marc' },
    { id: 3, name: 'kim' }
;]

I want to find all the objects with the same id and collect as one index in array, like this:

myNewArray = [
    [{ id: 1, name: 'sara' }, { id: 1, name: 'linda' }],
    [{ id: 2, name: 'json' }, { id: 2, name: 'marc' }],
    [{ id: 3, name: 'elin' }, { id: 3, name: 'kim' }]
];
    

Thanks a lot.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

const answer = new Map();
myNewArray.forEach(item=>{const value = answer.get(item.id) ??[];value.push(item);answer.set(item.id,value);});
myNewArray = [...answer.values()];
about 4 years ago · Santiago Gelvez 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!