0

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)?

2
  • 1
    Research the reduce method and think about what you could do with aggregated values that aren't just sums and such, but other arrays or objects... Commented Dec 19, 2021 at 11:37
  • Why aren't you "allowed to define new array"? Commented Dec 19, 2021 at 11:39

2 Answers 2

2

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);

Sign up to request clarification or add additional context in comments.

1 Comment

this might do the job but i find it extremely unreadable. good luck to the poor developer who inherits this code and tries to figure out what it does (which is likely going to be the OP in 6 months).
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.