es6 remove duplicate objects from array code example

Example 1: how to remove duplicate array object in javascript

let person = [{name: "john"}, {name: "jane"}, {name: "imelda"}, {name: "john"}];

function removeDuplicates(data, key) {
  
  return [
    ...new Map(data.map(item => [key(item), item])).values()
  ]

};

console.log(removeDuplicates(person, item => item.name));

Example 2: how to remove duplicate array object in javascript

let person = [
{name: "john"}, 
{name: "jane"}, 
{name: "imelda"}, 
{name: "john"},
{name: "jane"}
];

const data = Array.from(new Set(person.map(JSON.stringify))).map(JSON.parse);
console.log(data);

Example 3: remove duplicate array es6

let a = [10,20,30,10,30];
let b = a.filter((item,index) => a.indexOf(item) === index);
console.log(b);

Example 4: remove duplicate array es6

let a = [10,20,30,10,30];
let b = [... new Set(a)];
console.log(b);

Tags:

Css Example