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

153
Views
How to get the duplicates objects in an array?

I have an array like this:

    var clients=[{"id":1,"name":"john","age":20},
{"id":3,"name":"dean","age":23},
{"id":12,"name":"harry","age":14},
{"id":1,"name":"sam","age":22},
{"id":13,"name":"Bolivia","age":16},
{"id":7,"name":"sabi","age":60},
{"id":7,"name":"sahra","age":40},
{"id":4,"name":"natie","age":53},{"id":7,"name":"many","age":22}]

I want to find the duplicate objects and cluster them like this:

 [
       {
       "id":1,
        "clients":[
                    {"id":1,"name":"john","age":20},
                    {"id":1,"name":"sam","age":22}
                   ]
       },
     {
       "id":7,
       "clients":[
                   {"id":7,"name":"sabi","age":60},
                   {"id":7,"name":"sahra","age":40},
                   {"id":7,"name":"many","age":22}
                  ]
      }
    ]

can I do that with filter() like this:clients.reduce(//code hier)?

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

0

reduce() is tailor made for this. When you want to aggregate over an array and get a computed result, you should use reduce().

find() is another array method, which helps in finding an array element based on a condition (here the matching of id property).

var clients=[{"id":1,"name":"john","age":20},
{"id":3,"name":"dean","age":23},
{"id":12,"name":"harry","age":14},
{"id":1,"name":"sam","age":22},
{"id":13,"name":"Bolivia","age":16},
{"id":7,"name":"sabi","age":60},
{"id":7,"name":"sahra","age":40},
{"id":4,"name":"natie","age":53},{"id":7,"name":"many","age":22}]


let ans = clients.reduce((agg,x,index) => {
 let findI = agg.find( a => 
  a.id === x.id
 );
 if(findI) findI.clients.push(x);
 else {
   agg.push({
     id : x.id,
     clients : [x] 
   });
 }
 return agg;
},[]);

console.log(ans);

about 4 years ago · Juan Pablo Isaza Report

0

The simplest solution would be to loop over the clients and check for an existing object with the same id. If yes, push to clients array. Or else, just create one.

var clients = [{ "id": 1, "name": "john", "age": 20 },
{ "id": 3, "name": "dean", "age": 23 },
{ "id": 12, "name": "harry", "age": 14 },
{ "id": 1, "name": "sam", "age": 22 },
{ "id": 13, "name": "olivia", "age": 16 },
{ "id": 7, "name": "sabi", "age": 60 },
{ "id": 7, "name": "sahra", "age": 40 },
{ "id": 4, "name": "natie", "age": 53 }, { "id": 7, "name": "kany", "age": 22 }]

const groups = [];

for (let client of clients) {
  const existingGroup = groups.find(group => group.id == client.id)
  if (existingGroup)
    existingGroup.clients.push(client);
  else {
    groups.push({ id: client.id, clients: [client] });
  }
}

console.log(groups);

You can reassign the original object with the temporary object just used for this, and continue with your business logic, which I believe is the one you are looking for.

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!