Created
October 3, 2017 06:29
-
-
Save michaelgiles/83853bd3c64a515812557e5aec51f3ff to your computer and use it in GitHub Desktop.
Remove duplicates from array of objects
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
// Nice function to remove duplicates | |
function removeDuplicates(arr, key) { | |
if (!(arr instanceof Array) || key && typeof key !== 'string') { | |
return false; | |
} | |
if (key && typeof key === 'string') { | |
return arr.filter(function(obj, index, arr) { | |
return arr.map(function(mapObj) { | |
return mapObj[key]; | |
}).indexOf(obj[key]) === index; | |
console.log() | |
}); | |
} else { | |
return arr.filter(function(item, index, arr) { | |
return arr.indexOf(item) == index; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment