6
2

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 5 years have passed since last update.

{}(ブレース)を使わずにアロー関数で副作用を扱う方法

Last updated at Posted at 2018-01-16

 デバッグ時のみ、ログ出力したい場合などに用います。returnを書かなくて済むので、修正が少しだけ簡単になります。
 また、reduceで配列やオブジェクトを変更しながら、ループする場合などにも使用できます。

// かっこ()で囲んで、カンマでそのままの値を返す
[1, 2, 3, 4].filter(v => (console.log(v), v % 2 === 0)); //1 2 3 4
[1, 2, 3, 4].reduce((a, v) => (a[v] = v, a), {}); // {"1":1,"2":2,"3":3,"4":4} 
6
2
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
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?