Skip to content

Instantly share code, notes, and snippets.

Created October 1, 2013 06:51
Show Gist options
  • Save anonymous/6774726 to your computer and use it in GitHub Desktop.
Save anonymous/6774726 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
</body>
</html>
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