0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

#javascript の some / every の挙動を #node で確認する

Last updated at Posted at 2020-04-10
$ node

some

// 配列の中の一つでも値が1であればtrue
> [1,2,3].some(value => value == 1)
true
> [3,4,5].some(value => value == 1)
false

複数条件をつなげることも出来る

// 配列の中の一つでも値が1または2の要素があればtrue

> [1,2,3].some(value => value == 1 || value == 2)
true
> [2,3,4].some(value => value == 1 || value == 2)
true
> [3,4,5].some(value => value == 1 || value == 2)
false

every

// 配列の全ての値が1または2または3であればtrue
> [1,2,3].every(value => value == 1 || value == 2 || value == 3)
true
> [2,3,4].every(value => value == 1 || value == 2 || value == 3)
false

複数行 + return + ブロック句で書いてみる例

// 配列のすべてが
// 10-20の範囲で、2の倍数かどうか

const result1 = [20,22].every(
  value => {
    if(value < 10) { return false; }
    if(value >20) { return false; }
    if(value % 2 == 0) { return true; }
  }
)

console.log(result1) // false

const result2 = [10,14,18].every(
  value => {
    if(value < 10) { return false; }
    if(value > 20) { return false; }
    if(value % 2 == 0) { return true; }
  }
)

console.log(result2) // true

参考

JavaScriptのsomeとeveryがすごく便利 - Qiita

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?