I'm trying to remove all instances of duplicate objects in a javascript array. The helper function below works well for removing duplicates and keeping 1 instance, but I want to basically disqualify any object that has an equal property to another object by removing all instances.
function removeDuplicates(data, key) {
return [
... new Map(
data.map(x => [key(x), x])
).values()
]
}
Example of uncleaned array:
[
{
name: 'name1',
value: 15
},
{
name: 'name2',
value: 16
},
{
name: 'name3',
value: 17
},
{
name: 'name1',
value: 18
}
]
Returned from above helper function:
[
{
name: 'name1',
value: 15
},
{
name: 'name2',
value: 16
},
{
name: 'name3',
value: 17
}
]
What I'd like to be returned:
[
{
name: 'name2',
value: 16
},
{
name: 'name3',
value: 17
}
]
Would appreciate any ideas!
values are the same? Is it just thenamekey? Can you use the code here but invert the condition and changeidtonameto match your key, i.e.values.filter(e => !lookup[e.name])?