Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!