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

230
Visualizações
Select all similar objects (by select keys) from array?

How can I filter an array down to all objects that might look similar by certain keys without knowing the value of those keys?

Imagine I have an object like this:

let list = [
  { name: 'Adam',    age: '10', uniqueStuff: 'a'},
  { name: 'Brian',   age: '2',  uniqueStuff: 'b'},
  { name: 'Carley',  age: '15', uniqueStuff: 'c'},
  { name: 'Adam',    age: '2',  uniqueStuff: 'd'},
  { name: 'Christy', age: '15', uniqueStuff: 'e'},
  { name: 'Adam',    age: '10', uniqueStuff: 'f'},
]

I want the result to be:

let list = [
  { name: 'Adam',    age: '10', uniqueStuff: 'a'},
  { name: 'Adam',    age: '10', uniqueStuff: 'f'},
]

These may look like duplicates because they share the same name and age, but uniqueStuff is different. So I'm not trying to remove possible duplicates, but to get an array of all similar objects in the array.

Kind of like answering the question "how many kids do we have with the same names and same ages?"

This would be similar to MySQL's "having count(*) > 1" queries.

I'm having a really hard time searching for an answer or figuring out a LoDash method. Everything I find is about removing duplicates or getting only the duplicates so there's only 1 of each item. Instead, I want to get all the items that are duplicates and have duplicates. So each "match" in my array should have 2 or more items. For example, 2 or more 10-year old Adams, 2 or more 6-year-old Amys, and such.

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

const reduced = list.reduce((prev, curr) => {
    const key = `${curr.name}, ${curr.age}`
    return {
        ...prev,
        [key]: [...(prev[key] || []), curr]
    }
}, {})

const filtered = Object.values(reduced).filter(v => v.length > 1)

const flatted = filtered.flat()

You can remove the last line if you prefer having each set of 2 in an array.

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