Photo by Kelly Sikkema on Unsplash
JavaScript: Remove duplicates from an array of objects based on key
Table of contents
No headings in the article.
var users = [
{
name: 'Rahul', email: 'test1@test.com'
},
{
name: 'Rajeev', email: 'test2@test.com'
},
{
name: 'Revanth', email: 'test2@test.com'
}
];
let emailIds = users.map( (item) => {
return item.email;
})
let filtered = users.filter ( (item, index) => {
return !emailIds.includes(item.email, index+1);
})
console.log(filtered);
Output:
[
{ name: 'Rahul', email: 'test1@test.com' },
{ name: 'Revanth', email: 'test2@test.com' }
]