18
11

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.

foreachとmapの使い分け

Last updated at Posted at 2015-04-05

どうもforeachとmapの違いがわからなかったので整理

どういう時に使うか
foreachは配列の要素数だけ要素を使った処理を行いたいときと、その配列自身を操作したいときに使う
mapはある配列から新しい配列を作りたいときに使う

機能的な違いは?
foreach:配列を巡回して、値、インデックス、配列自信をもとに関数を実行する。返り値は特に無し
map:配列を巡回して値を元に関数を実行する。実行関数の返り値から新しい配列を作る

見返してみると英語の意味がそのままですね。

18
11
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
18
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?