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