Giner28
Giner28

Reputation: 35

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

Upvotes: 1

Views: 16460

Answers (3)

Nazeh Taha
Nazeh Taha

Reputation: 85

The Map object holds key-value pairs and remembers the original insertion order of the keys.

   const arr = [
    { name: "George", lastname: "GeorgeLast", age: 12 },
    { name: "George", lastname: "GeorgeLast", age: 13 },
    { name: "Bob", lastname: "GeorgeLast", age: 12 }
    ];
    
    const newMap = new Map();
    arr.forEach((item) => newMap.set(item.name, item));
    console.log([...newMap.values()]);

Upvotes: 1

Nikita Skrebets
Nikita Skrebets

Reputation: 1538

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)

Upvotes: 1

Wyck
Wyck

Reputation: 11750

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.

Upvotes: 4

Related Questions