0
0

More than 3 years have passed since last update.

JS~forEach~

Last updated at Posted at 2021-04-05

今回は配列の繰り返し処理のforEach文について学習していきます。

まず、一般的使わえる繰り返し分は以下になります。

index.js
<script>
    'use strict'

    const cities = ["東京", "大宮", "横浜"]

    for (let i=0; i<cities.length; i++) {
      document.write(city[i]);
    }
  </script>

*length ▶主に文字列の長さや配列の要素数を取得することができるプロパティになります。

この記述でもいいのですがもう少し簡単に書くことができます。

index.js
 cities.forEach((city,index) => {
    document.write(`都市${index}: ${city}`);
    });

forEachの引数には、cityとindexを入れました。
そして下の方でそれぞれを呼び出してもらいます。

要素の数がなくなるまで、繰り返し処理することができます👍

スクリーンショット 2021-04-05 13.03.08.png

0
0
1

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