配列したい(Array

1// 配列リテラル
2const array = ["a1", "a2", "a3"];

[]リテラルで配列を初期化できます。

1const array = new Array("a1", "a2", "a3");

new Array()コンストラクタでも配列を初期化できます。

値を追加したい(Array.push

1// 空の配列を作成し、値を追加する
2const array = new Array();
3array.push("a1");
4array.push("a2");
5array.push("a3");

.pushで配列に値を追加できます。

1// 配列の要素を取得
2console.log(array[0]);  // -> a1
3console.log(array[1]);  // -> a2
4console.log(array[3]);  // -> a3

配列のインデックスを指定して、要素を取得できます。

1array.length;  // -> 3
2for (let i = 0; i < array.length; i++ ) {
3    const item = array[i];
4    console.log(item);
5}

.lengthで配列の長さを取得できます。

結合したい(Array.concat

1const array1 = ["a", "b", "c"];
2const array2 = [1, 2, 3];
3const array3 = ["7", "8", "9"];
4const array4 = array1.concat(array2, array3);
5console.log(array4);
6// -> ["a", "b", "c", 1, 2, 3, "7", "8", "9"];

concatメソッドで配列を連結できます。 複数の配列を連結できます。

1const array5 = [...array1, ...array2];
2console.log(array5);
3// -> ["a", "b", "c", 1, 2, 3];

スプレッド演算子(...配列)でも連結できます。

ループしたい

1// for...ofループ
2for (const item of arrays) {
3    console.log(item);
4}

forEachしたい

1// forEachメソッド
2const newArrays = arrays.forEach(item => {
3    // 処理
4    console.log(item)
5})

mapしたい

1// mapメソッド
2const newArrays = arrays.map(item => {
3    // 処理
4    return 結果;
5})

mapメソッドで配列の要素に対して、同じ処理を適用できます。

1// 平方根を計算したい
2const numbers = [1, 4, 9, 16];
3const sqrtNumbers = numbers.map(Math.sqrt);
4console.log(sqrtNumbers);
5// -> [1, 2, 3, 4];

filterしたい

1// filterメソッド
2const newArrays = arrays.filter(callbackFn);
3
4const newArrays = arrays.filter(item => {
5    // フィルター処理
6    return 条件;
7})

filterメソッドを使って、配列から条件にマッチした要素を抽出できます。

1// 正の数を取得したい
2const numbers = [1, -4, 9, -16];
3const positives = numbers.filter(num => num > 0);
4console.log(positives);
5// -> [1, 9];

reduceしたい

1const newScalar = arrays.reduce(callbackFn, initialValue);
 1const numbers = [10, 20, 30, 40];
 2const sum = numbers.reduce((left, right) => left + right, 0);
 3// step1. 初期値を 0 に設定
 4// step2. left=0, right=10 で計算 -> 10
 5// step3. left=直前の値, right=20 で計算 -> 30
 6// step4. left=直前の値, right=30 で計算 -> 60
 7// step5. left=直前の値, right=40 で計算 -> 100
 8const average = sum / numbers.length;
 9console.log(sum);  // -> 100
10console.log(average);  // -> 25

flatしたい

1const array1 = [1, 2, [3, 4]];
2const array2 = array1.flat();
3console.log(array2);
4// -> [1, 2, 3, 4];

flatメソッドで配列を平坦化できます。 引数に平坦化の深さを指定できます。 平坦化する際、配列の空要素は削除されます。

最大値・最小値したい(Math.max / Math.min

1const numbers = [10, 20, 5, 40];
2const max = Math.max(...numbers);
3const min = Math.min(...numbers);

Math.maxMath.minとスプレッド演算子(...配列名)を使って、 最大値、最小値を取得できます。

配列同士の演算したい

1left = [1, 2, 3];
2right = [4, 5, 6];
3const added = left.map((value, index) => value + right[index]);
4const subtracted = left.map((value, index) => value - right[index]);
5const multiplied = left.map((value, index) => value * right[index]);
6const divided = left.map((value, index) => value / right[index]);
7const modulo = left.map((value, index) => value % right[index]);
8const powered = left.map((value, index) => Math.pow(value, right[index]));

配列同士の演算はビルトインされていないので、mapメソッドを使って自分で定義します。

1function addLists(left, right) {
2    if (left.length !== right.length) {
3        throw new Error("Arrays must have the same length.");
4    }
5    return left.map((value, index) => value + right[index]);
6}

それぞれ関数にしておくとよさそうです。

 1const arrays = [
 2    [1, 2, 3],
 3    [4, 5, 6],
 4    [7, 8, 9]
 5];
 6const result = arrays.reduce((left, right) => AddLists(left, right));
 7// step1. arrays[0] を初期値として使用
 8// step2. left=arrays[0], right=arrays[1] を計算
 9//    -> [5, 7, 9]
10// step3. left=直前の結果, right=arrays[2] を計算
11//    -> [12, 15, 18]

複数の配列を処理する場合reduceメソッドを利用すると簡潔にかけます。

リファレンス