I've got a long array of objects (>10_000) with duplicate objects I would like to remove.
In order to locate a duplicate I've got to look at two object properties: a, b
There are some elegant answers for removing objects by one property here: JavaScript: Remove duplicates of objects sharing same property value
e.g.
const uniq = _.uniq(arr, ele => ele.value});
Here is what the output of a solution would look like:
const arr = [{a:1, b:1}, {a:1, b:1}, {a:2, b:2}];
const removeDuplcatesByTwoKeys = (arr, ['a', 'b']) => // only elements that are duplicates for both key values;
result: const arr = [{a:2, b:2}];
I've tried _.uniq(arr, ele => ele.value && ele.otherValue});
but this does not work.
Another approach would be creating a map of the existing values keyed by those values e.g.
function unique(arr, keyProps) {
let map = new Map();
const kvArray = arr.map(entry => {
return keyProps.map(k => entry[k]).join('|');
})
kvArray.map(kv => {
if(map.has(kv)) {
const val = map.get(kv)
map.set(kv, val + 1)
} else {
map.set(kv, 1)
}
})
}
Though this would tell you what the duplicates are, what is the best way to remove them from the original array? This feels like a solution that is more complicated than it needs to be.
What is a performant way to remove duplicates by two properties from an array of objects?