Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda