javascript for ofで二次元配列の要素を取得するサンプル

環境
Windows 10 home 64bit
Google Chrome 107.0.5304.122(Official Build) (64 ビット)

構文
const 2次元配列名 =[[要素1,要素2,
for (let 変数名 of 2次元配列名){処理コード}
「for of」を使って二次元配列の要素を取得します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const cityArr = [['tokyo', 'oosaka', 'fukuoka'],['toto', 'kaka', 'too']];
for (let item of cityArr) {
console.log(item);
}
const cityArr = [['tokyo', 'oosaka', 'fukuoka'],['toto', 'kaka', 'too']]; for (let item of cityArr) { console.log(item); }
const cityArr = [['tokyo', 'oosaka', 'fukuoka'],['toto', 'kaka', 'too']];
 
for (let item of cityArr) {
   console.log(item);
}

実行結果
> Array [“tokyo", “oosaka", “fukuoka"]
> Array [“toto", “kaka", “too"]

JavaScript

Posted by arkgame