Skip to content

Instantly share code, notes, and snippets.

@michaelgiles
Created October 3, 2017 06:29
Show Gist options
  • Save michaelgiles/83853bd3c64a515812557e5aec51f3ff to your computer and use it in GitHub Desktop.
Save michaelgiles/83853bd3c64a515812557e5aec51f3ff to your computer and use it in GitHub Desktop.
Remove duplicates from array of objects
// 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