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

156
Views
Remove duplicate objects from an array with Keys

How to filter array of objects by property? for example in this array if two or more objects have same properties like name and lastname I want to remove either of them and leave only unique one in an array. example arr:

    [ {name: "George", lastname: "GeorgeLast", age: 12},
      {name: "George", lastname: "GeorgeLast", age: 13},
      {name: "Bob", lastname: "GeorgeLast", age: 12}] 

result should be either

 [ {name: "George", lastname: "GeorgeLast", age: 13},
   {name: "Bob", lastname: "GeorgeLast", age: 12}]

or

 [ {name: "George", lastname: "GeorgeLast", age: 12},
   {name: "Bob", lastname: "GeorgeLast", age: 12}]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Apply the technique shown in this answer, which is:

function onlyUnique(value, index, self) {
  return self.indexOf(value) === index;
}

...but using findIndex with some criteria rather than just indexOf.

let people = [
  { name: "George", lastname: "GeorgeLast", age: 12 },
  { name: "George", lastname: "GeorgeLast", age: 13 },
  { name: "Bob", lastname: "GeorgeLast", age: 12 }
]

let result = people.filter(
  (person, index) => index === people.findIndex(
    other => person.name === other.name
      && person.lastname === other.lastname
  ));
console.log(result);

As for whether it keeps 12-year-old George or 13-year-old George, it is a matter of how findIndex works, which so happens to return the first matching element. So in your example case it will keep 12-year-old George.

about 4 years ago · Juan Pablo Isaza Report

0

Another solution.
Here you don't need to iterate through the list n*n/2 times (if I count correctly).
On the other hand, this one looks less concise and uses more memory.
Use whichever you prefer.

const arr = [
  {name: "George", lastname: "GeorgeLast", age: 12},
  {name: "George", lastname: "GeorgeLast", age: 13},
  {name: "Bob", lastname: "GeorgeLast", age: 12}
];

const obj = {}
arr.forEach(v => {
  if (!obj[v.name]) {
    obj[v.name] = {}
  }
  if (!obj[v.name][v.lastname]) {
    obj[v.name][v.lastname] = v;
  }
})

const result = [];
Object.values(obj).forEach(nameObj =>
  Object.values(nameObj).forEach(surnObj => result.push(surnObj))
);

console.log(result)

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!