Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda