Last active
December 21, 2015 04:49
-
-
Save kazua/6252336 to your computer and use it in GitHub Desktop.
JavaScriptのArrayの拡張3
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
| //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() { | |
| if (this.length == 0) | |
| return this; | |
| var na = []; | |
| for ( var i = 0; i < this.length; i++) | |
| if (!na.contains(this[i])) | |
| na.push(this[i]); | |
| return na; | |
| }; | |
| } | |
| if (!Array.prototype.zip) { | |
| Array.prototype.zip = function(array) { | |
| var len = this.length > array.length ? array.length : this.length; | |
| if (len == 0) | |
| return []; | |
| var na = []; | |
| for ( var i = 0; i < len; i++) | |
| na.push([ this[i], array[i] ]); | |
| return na; | |
| }; | |
| } | |
| if (!Array.prototype.zipWithIndex) { | |
| Array.prototype.zipWithIndex = function() { | |
| if (this.length == 0) | |
| return this; | |
| var na = []; | |
| for ( var i = 0; i < this.length; i++) | |
| na.push([ this[i], i ]); | |
| return na; | |
| }; | |
| } | |
| if (!Array.prototype.takeWhile) { | |
| Array.prototype.takeWhile = function(fnc) { | |
| if (this.length == 0) | |
| return this; | |
| var na = []; | |
| for ( var i = 0; i < this.length; i++) { | |
| if (!fnc(this[i])) | |
| break; | |
| na.push(this[i]); | |
| } | |
| return na; | |
| }; | |
| } | |
| if (!Array.prototype.dropWhile) { | |
| Array.prototype.dropWhile = function(fnc) { | |
| if (this.length == 0) | |
| return this; | |
| var na = []; | |
| for ( var i = 0; i < this.length; i++) | |
| if (!fnc(this[i])) | |
| return this.slice(i); | |
| }; | |
| } | |
| if (!Array.prototype.head) { | |
| Array.prototype.head = function() { | |
| return this.slice(0, 1); | |
| }; | |
| } | |
| if (!Array.prototype.tail) { | |
| Array.prototype.tail = function() { | |
| return this.slice(1); | |
| }; | |
| } | |
| if (!Array.prototype.last) { | |
| Array.prototype.last = function() { | |
| return this.slice(this.length - 1, this.length); | |
| }; | |
| } | |
| if (!Array.prototype.init) { | |
| Array.prototype.init = function() { | |
| return this.slice(0, this.length - 1); | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment