Skip to content

Instantly share code, notes, and snippets.

@muhammadawaisshaikh
Created February 6, 2020 09:03
Show Gist options
  • Select an option

  • Save muhammadawaisshaikh/adf27bd64408419529f33730ad378675 to your computer and use it in GitHub Desktop.

Select an option

Save muhammadawaisshaikh/adf27bd64408419529f33730ad378675 to your computer and use it in GitHub Desktop.
filter unique items from array of objects
filterUniqueItems(arr) {
let mymap = new Map();
let uniqueItems = arr.filter(el => {
const val = mymap.get(el.text);
if(val) {
if(el.type < val) {
mymap.delete(el.text);
mymap.set(el.text, el.type);
return true;
} else {
return false;
}
}
mymap.set(el.text, el.type);
return true;
});
return uniqueItems;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment