Arrays
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
Arrays hold lists
- An array is an ordered list of values in
[ ... ]. - Reach an item by its index, counting from
0:arr[0]is the first. .lengthtells you how many items there are.
日本語
配列はリストを保持する
- 配列とは、
[ ... ]に格納された順序付けられた値のリストです。 - インデックスによって項目にアクセスし、
0から数えます:arr[0]が最初です。 .lengthは項目の総数を返します。
let nums = [10, 20, 30];
console.log(nums[0]); // → 10
console.log(nums.length); // → 3
English
Add and remove items
.push(x)addsxto the end;.pop()removes the last item.- The array grows and shrinks as you go.
日本語
項目を追加・削除する
.push(x)はxを末尾に追加し、.pop()は最後尾の項目を削除します。- 操作するたびに配列は拡大したり縮小したりします。
let nums = [10, 20, 30];
nums.push(40);
console.log(nums); // → [10,20,30,40]
nums.pop();
console.log(nums); // → [10,20,30]
Explore · 探索
Index, push and pop
Items are numbered from 0. push adds to the end; pop removes from the end.
English
Loop over an array
- A
forloop with an index visits each item, from0tolength - 1.
日本語
配列をループで処理する
- インデックスを持つ
forループは、0からlength - 1まで各項目を順に処理します。
let nums = [1, 2, 3];
for (let i = 0; i < nums.length; i++) {
console.log(nums[i]); // → 1, then 2, then 3
}
English
Common mistakes
- Index from
0;a.lengthis the size. pushadds to the end;popremoves from the end.
日本語
よくあるミス
- インデックスは
0から始め、a.lengthがサイズです。 pushは末尾に追加し、popは末尾から削除します。
English
Now you try
- Use an index, or a loop, on each array.
- Press Check answer to test it.
日本語
あなたも試してみよう
- 各配列に対してインデックスまたはループを使用してください。
- Check answer を押して確認する。
let colours = ["red", "green", "blue"]. Log the second item, colours[1] (it is green).
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
let nums = [4, 8, 15, 16]. Log how many items it has with .length (it is 4).
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
let stack = [1, 2]. Use .push to add 3, then log the array (it is [1,2,3]).
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Use a for loop to add up every number in nums and log the total (it is 43).
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。