Skip to content

Instantly share code, notes, and snippets.

@rexso
Created October 16, 2015 14:31
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 rexso/52c5efa99c26485375b7 to your computer and use it in GitHub Desktop.
Save rexso/52c5efa99c26485375b7 to your computer and use it in GitHub Desktop.
JS Function to filter array of objects
function objArrUniqKeys(arr, key) {
if(arr instanceof Array) {
var keys = [];
return arr.filter(function(elem, idx, arr) {
if(typeof elem == "object") {
if(elem.hasOwnProperty(key) && keys.indexOf(elem[key]) == -1) {
keys.push(elem[key]);
return true;
}
}
return false;
});
}
return arr;
}
var a = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 1 }, { id: 3 }];
a = objArrUniqKeys(a, "id");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment