JavaScript: Remove duplicates from an array of objects based on key

1 min read
Table of contents

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' }
]
0
Subscribe to my newsletter
Read articles from Hari Krishna Anem directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Hari Krishna Anem
Hari Krishna Anem
Full stack developer (ReactJS, NodeJS, JavaScript, PHP, SQL)