Created
September 19, 2011 13:22
-
-
Save ishiduca/1226478 to your computer and use it in GitHub Desktop.
Array#uniq 配列の重複した値を間引いた新しい配列を返す
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
// 配列の重複した値を間引いた新しい配列を返す | |
if (! Array.prototype.uniq) { | |
Array.prototype.uniq = function () { | |
var that = this.slice(0), len = that.length, storage = {}; | |
for (len--; len >= 0; len--) { | |
if (! storage[that[len]]) { | |
storage[that[len]] = true; | |
} else { | |
that.splice(len, 1); | |
} | |
} | |
return (that.length === 0) ? undefined : that; | |
}; | |
} | |
var org = ('abc def 123 46 0 ghi def GIHUN data dog beta beta 46 69').split(/\s+/); | |
console.log(org); | |
var uniq = org.uniq(); | |
console.log(uniq); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment