Skip to content

Instantly share code, notes, and snippets.

@assaftenen
Created February 19, 2017 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save assaftenen/84656c50e8305d45d938e1d83f24d091 to your computer and use it in GitHub Desktop.
Save assaftenen/84656c50e8305d45d938e1d83f24d091 to your computer and use it in GitHub Desktop.
filter duplicate objects from (small) arrays using a key
function filterDuplicates(arr,key) {
const set = new Set(arr.map(x=>x[key]))
const newArray = []
while (set.size > 0) {
const item = arr.filter(x => set.has(x[key]))[0]
set.delete(item[key])
newArray.push(item)
}
return newArray
}
/// const arr = [{id:1},{id:1},{id:2}]
/// filterDuplicates(arr,'id')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment