Skip to content

Instantly share code, notes, and snippets.

@divanvisagie
Forked from anonymous/jsbin.iqoxOzE.html
Last active December 24, 2015 09:08
Show Gist options
  • Save divanvisagie/6774729 to your computer and use it in GitHub Desktop.
Save divanvisagie/6774729 to your computer and use it in GitHub Desktop.
var testArr = ['a','a','b','c','d'];
var numArr = [1,1,2,3,4,4];
var objArr = [{},{},{test:0},{test:5},{test:0}];
function unique( arr ){
var tmp = {};
var key;
for(var i=0; i < arr.length; i++){
if (typeof arr[i] === 'object'){
key = JSON.stringify(arr[i]);
}
else{
key = arr[i];
}
tmp[key] = arr[i];
}
return Object.keys(tmp).map(function(elem,idx,arr){
return tmp[elem];
});
}
console.log(unique(testArr));
console.log(unique(numArr));
console.log(unique(objArr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment