Remove duplicate data from array object in javascript

Edison Devadoss
2 min readJan 6, 2019

--

Before we are going to remove duplicate value from the array of object we should know to remove duplicate value from the array.

First, define the array of data.

let words = ['Edison','Stalin','Stalin','Edison'];

In this array, we have two duplicate values. One ‘Edison’ another one is ‘Stalin’.

Remove duplicate value using filter method.

words.filter((v,i)=> words.indexOf(v) === i);
console.log(words);
//expected output is ['Edison', 'Stalin'];

So far it easy and well. But how we remove duplicate data from the array object?

Remove the duplicate value from the array object.

Let us define an array of object.

var words = [{name: 'Edison'}, {name: 'Stalin'}, {name: 'Stalin'}, {name:'Edison'}];

In this array, we have two duplicate values. One {name: ‘Edison’} another one is {name: ‘Stalin’}.

In above all value store in name object. In this example, we will see how to remove duplicate from the array object. Earlier I felt its really heard. Eventually, I found the solution.

We should use map() function its help to remove duplicate value. We should return name value using map() method then write indexOf() method.

const result = words.filter((v,i) => {
return words.map((val)=> val.name).indexOf(v.name) == i
})
console.log(result);
//Excepted output is [{name: 'Edison'},{name:'Stalin'}]

Thank you for reading dear friends.

Reference links:

--

--

Edison Devadoss

Software developer / JavaScript / React / React Native / Firebase / Node.js / C Programming / Book Reader