Skip to content

Instantly share code, notes, and snippets.

@yuba
Created April 20, 2016 09:02
Show Gist options
  • Save yuba/143cf0e89dc68791bdca714554e57f14 to your computer and use it in GitHub Desktop.
Save yuba/143cf0e89dc68791bdca714554e57f14 to your computer and use it in GitHub Desktop.
配列にfirst, lastメソッドを生やす ref: http://qiita.com/yuba/items/9b38fa15bc6b62e1ec71
/** 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