配列の作成
new Array(2)
new Array(2, 3, 4)
[2, 3, 4]
Array.of(2)
Array.of(2, 3, 4, 5)
Array.from([2, 3, 4])
Array.from([2, 3, 4], x => x * 2)
[2, 3, 4].entries()
[2, 3, 4].keys()
[2, 3, 4].values()
Array.from([2, 3, 4].entries())
Array.from([2, 3, 4].keys())
Array.from([2, 3, 4].values())
要素数の取得・設定
const a = [0, 1, 2]
a.length
a.length += 1
a
a.length -= 2
a
要素の参照
[0, 1, 2][0]
[0, 1, 2][-1]
[0, 1, 2].at(0)
[0, 1, 2].at(-1)
const a = [0, 1, 2]
a[0] == a["0"]
a[0] == a["00"]
要素の選択・変換
[2, 3, 4].filter(x => x % 2 == 0)
[2, 3, 4].map(x => x % 2 == 0)
要素の平坦化
[[1, 2, 3], [4, 5, 6]].flat()
[1, 6].flatMap(x => [x, x + 1, x + 2])
要素を結合した文字列の作成
[1, 2, 3].join()
[1, 2, 3].join("-")
値による範囲上書き
[0, 0, 0, 0, 0].fill(2, 1)
[0, 0, 0, 0, 0].fill(2, 1, 3)
条件式による要素の検索
[1, 2, 3].find(x => x % 2 == 0)
[1, 2, 3].findIndex(x => x % 2 == 0)
[0, 1, 2].every(x => x % 2 == 0)
[2, 4, 6].every(x => x % 2 == 0)
[0, 1, 2].some(x => x % 2 == 0)
[2, 4, 6].some(x => x % 2 == 0)
[1, 3, 5].some(x => x % 2 == 0)
値による要素の検索
[1, 2, 3].includes(1)
[1, 2, 3].includes(1, 2)
[1, 2, 3].includes(0)
[1, 2, 2, 1].indexOf(1)
[1, 2, 2, 1].indexOf(1, 1)
[1, 2, 2, 1].indexOf(0)
[1, 2, 2, 1].indexOf(1, 4)
[1, 2, 2, 1].lastIndexOf(1)
[1, 2, 2, 1].lastIndexOf(0)
[1, 2, 2, 1].lastIndexOf(1, 2)
[1, 2, 2, 1].lastIndexOf(1, -2)
要素の集約
[1, 2, 3].reduce((prev, current) => prev + current)
[1, 2, 3].reduce((prev, current, i) => prev + current + i)
[1, 2, 3].reduce((prev, current, i, array) => console.log([prev, current, i, array]))
[1, 2, 3].reduceRight((prev, current) => prev+current)
[1, 2, 3].reduceRight((prev, current, i) => prev+current+i)
[1, 2, 3].reduceRight((prev, current, i, array) => console.log([prev, current, i, array]))
要素の追加・抽出
[1, 2, 3].concat([4, 5, 6])
[1, 2, 3].concat(4, 5, 6)
[1, 2, 3].concat([4, 5], 6)
const a = [1, 2, 3]
a.unshift(5, 6)
a
const b = [1, 2, 3]
b.shift()
b
const c = [1, 2, 3]
c.pop()
c
const d = [1, 2, 3];
d.push(4)
d
要素の内部複写
[0, 1, 2, 3, 4, 5, 6].copyWithin(3)
[0, 1, 2, 3, 4, 5, 6].copyWithin(3, 1)
[0, 1, 2, 3, 4, 5, 6].copyWithin(3, 1, 2)
要素の検索
[1, 2, 3, 4, 5].find(x => x > 2)
[1, 2, 3, 4, 5].findIndex(x => x > 2)
要素の抽出と追加
const a = [1, 2, 3, 4, 5]
a.splice(2)
a
const b = [1, 2, 3, 4, 5]
b.splice(2, 2)
b
const c = [1, 2, 3, 4, 5]
c.splice(2, 2, 8, 9, 10)
c
const d = [1, 2, 3, 4, 5]
d.splice(2, 0, 8, 9, 10)
d
参照とシャローコピー
const a = [1, 2, 3]
const b = a
const c = [...a]
const d = a.slice()
a[1] = 0
a
b
c
d
部分配列の作成
[1, 2, 3, 4, 5].slice(2)
[1, 2, 3, 4, 5].slice(-2)
[1, 2, 3, 4, 5].slice(1, 3)
[1, 2, 3, 4, 5].slice(1, -3)
[1, 2, 3, 4, 5].slice(-3, 1)
[1, 2, 3, 4, 5].slice(-3, -1)
[1, 2, 3, 4, 5].slice(-3, 5)
要素の関数適用
[1, 2, 3].forEach(x => console.log(x))
[1, 2, 3].forEach((x, index) => console.log([x, index]))
[1, 2, 3].forEach((x, index, array) => console.log([x, index, array]))
要素を文字列として結合
[0, 1, 2].join()
[0, 1, 2].join("-")
配列の判定
Array.isArray([])
Array.isArray([0, 1, 2])
Array.isArray(0)
Array.isArray({a, 0})
Array.isArray(Int32Array.of(0, 1))
並び替え
const a = [1, 0, 2]
a.sort()
a
a.reverse()
a
const b = [1, 0, 2]
b.sort((a, b) => b - a)
b
文字列変換
[0, 1, 2].toString()
[0, 1000].toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' })
[0, 1, 2][Symbol.iterator]
コンストラクタシンボル
Array[Symbol.species]
参考