I'm trying to get rid of duplicates in array of objects. I want my code linear so I'm using hashtable:
output = Array.from(new Set(output.map(e => e.company)))
.map(company => { return output.find(e => e.company === company) })
This code works properly for this array of objects:
let output = [
{ company: 'sony'},
{ company: 'sony'},
{ company: 'apple'}
]
but thing is that I want to reuse this code so I would like to move it to function which would take array output and company string. I don't know how to do that, I tried like this:
const uniq = (array, key) => {
return Array.from(new Set(array.map( e => e[key] )))
.map(key => { return array.find(e => e[key] === key) })
}
but this return array of two undefined.
Also second thing is that it's pretty complicated code for such trivial task. Isn't there some easier way to do same without losing performance?