Created
July 11, 2012 18:22
-
-
Save k2nr/3092157 to your computer and use it in GitHub Desktop.
remove duplicates in javascript
This file contains hidden or 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
function removeDups(array) { | |
var obj = {}, | |
len = array.length, | |
res = [], | |
i; | |
for(i=0; i < len; i++) { | |
obj[JSON.stringify(array[i])] = array[i]; | |
} | |
for(i in obj) { | |
res.push(obj[i]); | |
} | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
配列のunique化ですね。
ES5.1において、
JSON.stringify
はNaN
、Infinity
、-Infinity
を'null'
にします。ECMAScript Language Specification - ECMA-262 Edition 5.1
null
、NaN
、Infinity
、-Infinity
、+0
、-0
、Object
、RegExp
、Function
を厳密に区別すると以下の様な実装になりますね。