LoginSignup
7
0

More than 5 years have passed since last update.

underscoreのeachでbreakできないならeveryを使おう

Posted at

underscore.jsのeachを使っているとき、下記のようにしてもループは途中で終わりません。

_.each([1,2,3,4,5,6], function (elem) {
  console.log(elem)
  return false
})

出力
1
2
3
4
5
6

_.everyを使う

_.every([1,2,3,4,5,6], function (elem) {
  console.log(elem)
  return false
})
出力
1

Returns true if all of the values in the list pass the predicate truth test. Short-circuits and stops traversing the list if a false element is found.

デバッグなどでループを全部回したくない場合は_.everyを使うと便利かもしれません。

以上です。

7
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
0