i want to remove duplicates from javascript 'objects array' based on object property value
var data = [
{
"John Doe": "john33@gmail.com",
},
{
"William Smith": "william65@gmail.com",
},
{
"Robert Johnson": "robert99@gmail.com",
},
{
"John Smith": "john33@gmail.com",
},
{
"James Johnson": "james8@gmail.com",
},
];
here in the 'data' array there are same emails for "John Doe" and "John Smith", i want to remove one object of theme.
This can be done with Array.reduce(), combined with Object.values() as follows:
var data = [
{
"John Doe": "john33@gmail.com",
},
{
"William Smith": "william65@gmail.com",
},
{
"Robert Johnson": "robert99@gmail.com",
},
{
"John Smith": "john33@gmail.com",
},
{
"James Johnson": "james8@gmail.com",
},
];
const result = data.reduce((acc, o) => {
if (!acc.map(x => Object.values(x)[0]).includes(Object.values(o)[0])) {
acc.push(o);
}
return acc;
}, []);
console.log(result);