potisanのプログラミングメモ

趣味のプログラマーがプログラミング関係で気になったことや調べたことをいつでも忘れられるようにメモするブログです。はてなブログ無料版なので記事の上の方はたぶん広告です。記事中にも広告挿入されるみたいです。

JavaScript Arrayコード集(20220121)

配列の作成

new Array(2)
// [<2 empty slots>]
new Array(2, 3, 4)
// [2, 3, 4]
[2, 3, 4]
// [2, 3, 4]
Array.of(2)
// [2]
Array.of(2, 3, 4, 5)
// [2, 3, 4, 5]
Array.from([2, 3, 4])
// [2, 3, 4]
Array.from([2, 3, 4], x => x * 2)
// [4, 6, 8]

要素のイテレーター作成

[2, 3, 4].entries()             // Array iterator
[2, 3, 4].keys()                // Array iterator
[2, 3, 4].values()              // Array iterator
Array.from([2, 3, 4].entries()) // [[0, 2], [1, 3], [2, 4]]
Array.from([2, 3, 4].keys())    // [1, 2, 3]
Array.from([2, 3, 4].values())  // [2, 3, 4]

素数の取得・設定

const a = [0, 1, 2]
a.length // 3

a.length += 1
a // [0, 1, 2, <1 empty slot>]

a.length -= 2
a // [0, 1]

要素の参照

[0, 1, 2][0]     // 0
[0, 1, 2][-1]    // undefined
[0, 1, 2].at(0)  // 0
[0, 1, 2].at(-1) // 2

const a = [0, 1, 2]
a[0] == a["0"]  // true
a[0] == a["00"] // false

要素の選択・変換

[2, 3, 4].filter(x => x % 2 == 0) // [2, 4]
[2, 3, 4].map(x => x % 2 == 0)    // [true, false, true]

要素の平坦化

[[1, 2, 3], [4, 5, 6]].flat()            // [1, 2, 3, 4, 5, 6]
[1, 6].flatMap(x => [x, x + 1, x + 2]) // [1, 2, 3, 6, 7, 8]

要素を結合した文字列の作成

[1, 2, 3].join()   // "1,2,3"
[1, 2, 3].join("-") // "1-2-3"

値による範囲上書き

[0, 0, 0, 0, 0].fill(2, 1)    // [0, 2, 2, 2, 2]
[0, 0, 0, 0, 0].fill(2, 1, 3) // [0, 2, 2, 0, 0]

条件式による要素の検索

[1, 2, 3].find(x => x % 2 == 0)      // 2 (2 % 2 == 0)
[1, 2, 3].findIndex(x => x % 2 == 0) // 1 ([1, 2, 3][1] = 2)
[0, 1, 2].every(x => x % 2 == 0) // false
[2, 4, 6].every(x => x % 2 == 0) // true
[0, 1, 2].some(x => x % 2 == 0) // true
[2, 4, 6].some(x => x % 2 == 0) // true
[1, 3, 5].some(x => x % 2 == 0) // false

値による要素の検索

[1, 2, 3].includes(1)           // true
[1, 2, 3].includes(1, 2)        // false
[1, 2, 3].includes(0)           // false
[1, 2, 2, 1].indexOf(1)         // 0
[1, 2, 2, 1].indexOf(1, 1)      // 3
[1, 2, 2, 1].indexOf(0)         // -1
[1, 2, 2, 1].indexOf(1, 4)      // -1
[1, 2, 2, 1].lastIndexOf(1)     // 3
[1, 2, 2, 1].lastIndexOf(0)     // -1
[1, 2, 2, 1].lastIndexOf(1, 2)  // 0
[1, 2, 2, 1].lastIndexOf(1, -2) // 0

要素の集約

[1, 2, 3].reduce((prev, current) => prev + current)      // 6
[1, 2, 3].reduce((prev, current, i) => prev + current + i) // 9
[1, 2, 3].reduce((prev, current, i, array) => console.log([prev, current, i, array]))
// [1        , 2, 1, [1, 2, 3]]
// [undefined, 3, 2, [1, 2, 3]]

[1, 2, 3].reduceRight((prev, current) => prev+current)      // 6
[1, 2, 3].reduceRight((prev, current, i) => prev+current+i) // 7
[1, 2, 3].reduceRight((prev, current, i, array) => console.log([prev, current, i, array]))
// [3        , 2, 1, [1, 2, 3]]
// [undefined, 1, 0, [1, 2, 3]]

要素の追加・抽出

[1, 2, 3].concat([4, 5, 6]) // [1, 2, 3, 4, 5, 6]
[1, 2, 3].concat(4, 5, 6)   // [1, 2, 3, 4, 5, 6]
[1, 2, 3].concat([4, 5], 6) // [1, 2, 3, 4, 5, 6]

const a = [1, 2, 3]
a.unshift(5, 6) // 5
a               // [5, 6, 1, 2, 3]

const b = [1, 2, 3]
b.shift() // 1
b         // [2, 3]

const c = [1, 2, 3]
c.pop() // 3
c       // [1, 2]

const d = [1, 2, 3];
d.push(4) // 4
d         // [1, 2, 3, 4]

要素の内部複写

[0, 1, 2, 3, 4, 5, 6].copyWithin(3)       // [0, 1, 2, 0, 1, 2, 3]
[0, 1, 2, 3, 4, 5, 6].copyWithin(3, 1)    // [0, 1, 2, 1, 2, 3, 4]
[0, 1, 2, 3, 4, 5, 6].copyWithin(3, 1, 2) // [0, 1, 2, 1, 4, 5, 6]

要素の検索

[1, 2, 3, 4, 5].find(x => x > 2)      // 3
[1, 2, 3, 4, 5].findIndex(x => x > 2) // 2

要素の抽出と追加

const a = [1, 2, 3, 4, 5]
a.splice(2) // [3, 4, 5]
a           // [1, 2]

const b = [1, 2, 3, 4, 5]
b.splice(2, 2) // [3, 4]
b              // [1, 2, 5]

const c = [1, 2, 3, 4, 5]
c.splice(2, 2, 8, 9, 10) // [3, 4]
c                        // [1, 2, 8, 9, 10, 5]

const d = [1, 2, 3, 4, 5]
d.splice(2, 0, 8, 9, 10) // []
d                        // [1, 2, 8, 9, 10, 3, 4, 5]

参照とシャローコピー

const a = [1, 2, 3]
const b = a         // 参照
const c = [...a]    // シャローコピー
const d = a.slice() // シャローコピー
a[1] = 0
a // [1, 0, 3]
b // [1, 0, 3]
c // [1, 2, 3]
d // [1, 2, 3]

部分配列の作成

[1, 2, 3, 4, 5].slice(2)      // [3, 4, 5]
[1, 2, 3, 4, 5].slice(-2)     // [4, 5]
[1, 2, 3, 4, 5].slice(1, 3)   // [2, 3]
[1, 2, 3, 4, 5].slice(1, -3)  // [2]
[1, 2, 3, 4, 5].slice(-3, 1)  // []
[1, 2, 3, 4, 5].slice(-3, -1) // [3, 4]
[1, 2, 3, 4, 5].slice(-3, 5)  // [3, 4, 5]

要素の関数適用

[1, 2, 3].forEach(x => console.log(x))
// 1
// 2
// 3
[1, 2, 3].forEach((x, index) => console.log([x, index]))
// [1, 0]
// [2, 1]
// [3, 2]
[1, 2, 3].forEach((x, index, array) => console.log([x, index, array]))
// [1, 0, [1, 2, 3]]
// [2, 1, [1, 2, 3]]
// [3, 2, [1, 2, 3]]

要素を文字列として結合

[0, 1, 2].join()    // "0,1,2"
[0, 1, 2].join("-") // "0-1-2"

配列の判定

Array.isArray([])                  // true
Array.isArray([0, 1, 2])           // true
Array.isArray(0)                   // false
Array.isArray({a, 0})              // false
Array.isArray(Int32Array.of(0, 1)) // false

並び替え

// 各要素を文字列に変換してUTF-16コードの小さい順に並び替える。
const a = [1, 0, 2]
a.sort()    // [0, 1, 2]
a           // [0, 1, 2]
a.reverse() // [2, 1, 0]
a           // [2, 1, 0]

// 各要素を数値のまま大きい順に並び替える。
const b = [1, 0, 2]
b.sort((a, b) => b - a) // [2, 1, 0]
b                       // [2, 1, 0]

文字列変換

[0, 1, 2].toString() // "0,1,2"
[0, 1000].toLocaleString('ja-JP', { style: 'currency', currency: 'JPY' })
// "¥0,¥1,000"

イテレーターシンボル

[0, 1, 2][Symbol.iterator] // function values()

コンストラクタシンボル

Array[Symbol.species]      // function Array()

参考