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

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

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