Skip to content

Instantly share code, notes, and snippets.

@kazua
Last active December 19, 2015 16:39
Show Gist options
  • Save kazua/5985300 to your computer and use it in GitHub Desktop.
Save kazua/5985300 to your computer and use it in GitHub Desktop.
Arrayのcontainsとdistinct
//write kazua
if (!Array.prototype.contains) {
Array.prototype.contains = function(value) {
for ( var i = 0; i < this.length; i++)
if (this[i] === value)
return true;
return false;
};
}
if (!Array.prototype.distinct) {
Array.prototype.distinct = function() {
var t = Object(this), nt = [], len = t.length >>> 0;
if (len == 0)
return this;
for ( var i = 0; i < t.length; i++)
if (!nt.contains(t[i]))
nt.push(t[i]);
return nt;
};
}
function test() {
var a = new Array("1", "3", "4", "1", "5");
a = a.distinct();
alert(a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment