Created
April 20, 2016 09:02
-
-
Save yuba/143cf0e89dc68791bdca714554e57f14 to your computer and use it in GitHub Desktop.
配列にfirst, lastメソッドを生やす ref: http://qiita.com/yuba/items/9b38fa15bc6b62e1ec71
This file contains 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
/** Extends array object */ | |
interface Array<T> { | |
/** Returns the first element that satisfies the predicate */ | |
first(predicate:(element:T) => boolean, defaultVaule: T): T; | |
/** Returns the last element that satisfies the predicate */ | |
last(predicate:(element:T) => boolean, defaultVaule: T): T; | |
} | |
Array.prototype.first = function (predicate: (element) => boolean, defaultVaule) { | |
for (var i: number = 0; i < this.length; i++) { | |
if (predicate(this[i])) return this[i]; | |
} | |
return defaultVaule; | |
}; | |
Array.prototype.last= function (predicate: (element) => boolean, defaultVaule) { | |
for (var i: number = this.length; i-- > 0;) { | |
if (predicate(this[i])) return this[i]; | |
} | |
return defaultVaule; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment