Created
February 19, 2017 20:11
-
-
Save assaftenen/84656c50e8305d45d938e1d83f24d091 to your computer and use it in GitHub Desktop.
filter duplicate objects from (small) arrays using a key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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