I have two arrays in javascript, where i want to do two different operations
- Map an attribute upon each element in each lists
- filter out none unique values based on an attribute
I have this function so far
export function generateDisplayedLabels(systemLabels, masterState){
const mappedSystemlabels = systemLabels.map(label => Object.assign(label, {type: "system"}))
const mappedMasterlabels = masterState.map(label => Object.assign(label, {type: "master"}))
const displayedLabels = _.union(mappedSystemlabels, mappedMasterState);
return displayedLabels
}
This would work except for the fact, that whenever i map over the objects in the beginning, the "unique" elements are no longer unique, because they have another attribute mapped upon it. Is there a time efficient way, that i can filter out the none unique elements, ignoring the attribute, that have been mapped onto it.
[<>]snippet editor._.unionBywhich takes a function as a second parameter which lets you choose a field or value by which the uniqueness is computed.