Array#forEach は break できない

・Array.prototype.forEach は break できない ・Array.prototype.some は条件に一致したとき break する (true を返す) ・jQuery.each は return false で break する Array.prototype.forEach ( callbackfn [, thisArg] ) 続きを読む
2
think49 @think49

for文の代わりに Array.prototype.forEach を使ってみた。入れ子になるほど便利だけど、break に相当するステートメントはないのかな?

2010-10-17 04:41:35
think49 @think49

@think49 発想が間違っている気がしてきた。 Array.prototype.forEach で break するのではなく、条件に一致したら true を返すメソッドがあるのかもしれない。探してみよう。

2010-10-17 15:41:44
think49 @think49

@Constellation ありがとうございます。 Array.prototype.some ですね。これは便利! ES5 ( http://goo.gl/uYc8 ), MDC ( http://goo.gl/aFoT ) #JavaScript #ES5

2010-10-17 15:48:29
think49 @think49

@think49 jQuery には Array.prototype.some に相当する機能がなさそう…。 http://api.jquery.com/

2010-10-17 18:11:45
think49 @think49

@think49 http://code.jquery.com/jquery-1.4.3.js をダウンロードして "some" でgrep検索してみたが、それらしきコードはHITしなかった。 やはり、Array.prototype.some は実装されていない模様。

2010-10-17 18:32:19
think49 @think49

@uupaa なるほど…。each() で break できるように拡張されていたんですね。

2010-10-17 20:30:58
think49 @think49

@os0x おお、既に記事があったんですね。ありがとうございます。

2010-10-17 21:06:17