I have an array of objects in which most of the properties may be duplicates of each other, but one value may differ:
const originalArray = [
{ id: 1, name: 'x', 'description': 'x', url: 'y' },
{ id: 2, name: 'x', 'description': 'x', url: 'z' }
]
I want to dedupe this array based on a difference of name, but retain the URL differences as an array in the deduped array:
const dedupedArray = [
{ id: 1, name: 'x', 'description': 'x', url: ['y','z'] },
]
function removeDuplicatesByProperty(keyFn, array) {
const mySet = new Set();
return array.filter(function (x) {
const key = keyFn(x),
isNew = !mySet.has(key);
if (isNew) mySet.add(key);
return isNew;
});
}
const dedupedArray = removeDuplicatesByProperty((x) => x.name, originalArray);