Given an array of objects like the one below, I need to selectively filter out repetitive keys in order to address specific duplicates without removing all instances.
var arr = [
{id: 1, value: 'John'},
{id: 2, value: 'John'}, // Should be filtered
{id: 3, value: 'John'}, // Should be filtered
{id: 4, value: 'John'}, // Should be filtered
{id: 5, value: 'Alex'},
{id: 6, value: 'Louis'},
{id: 7, value: 'David'},
{id: 8, value: 'David'}, // Should not be filtered
]
Desired Result:
arr = [
{id: 1, value: 'John'},
{id: 5, value: 'Alex'},
{id: 6, value: 'Louis'},
{id: 7, value: 'David'},
{id: 8, value: 'David'},
]
I have attempted the following solution so far:
arr = arr.reduce((a, b) => {
if (!a.some(x => x.description === b.description)) a.push(b);
return a;
}, []);
Thank you in advance.