Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Created September 19, 2011 13:22
Show Gist options
  • Save ishiduca/1226478 to your computer and use it in GitHub Desktop.
Save ishiduca/1226478 to your computer and use it in GitHub Desktop.
Array#uniq 配列の重複した値を間引いた新しい配列を返す
// 配列の重複した値を間引いた新しい配列を返す
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