Answers for "remove duplicates from js array of objects"

7

remove duplicate objects from array javascript

const addresses = [...]; // Some array I got from async call

const uniqueAddresses = Array.from(new Set(addresses.map(a => a.id)))
 .map(id => {
   return addresses.find(a => a.id === id)
 })
Posted by: Guest on January-02-2020
0

Removing duplicates in an Array of Objects

// BEST ANSWER - LINEAR TIME SOLUTION

const seen = new Set();
const arr = [
  { id: 1, name: "test1" },
  { id: 2, name: "test2" },
  { id: 2, name: "test3" },
  { id: 3, name: "test4" },
  { id: 4, name: "test5" },
  { id: 5, name: "test6" },
  { id: 5, name: "test7" },
  { id: 6, name: "test8" }
];

const filteredArr = arr.filter(el => {
  const duplicate = seen.has(el.id);
  seen.add(el.id);
  return !duplicate;
});
Posted by: Guest on September-10-2020

Code answers related to "remove duplicates from js array of objects"

Code answers related to "Javascript"

Browse Popular Code Answers by Language