length | 配列の数を取得 |
例) let array = [‘a’, ‘b’, ‘c’] let a = array.length console.log(a); // 3 | |
array[] | 要素の取得:配列の中の要素の番号を指定して要素を取得する |
例)普通の配列の場合 let array = [‘a’, ‘b’, ‘c’] let item = array[0] console.log(item) // a 例)2次元配列の場合 let array = [ [‘a’, ‘b’, ‘c’], [‘d’, ‘e’, ‘f’], [‘g’, ‘h’, ‘i’] ] let item = array[0][1] console.log(item) // b | |
slice | 要素の取得:範囲指定して要素を取得する 例えば「配列の中の1〜3つの要素をすべて取得」などの指定ができる |
例) let array = [‘a’, ‘b’, ‘c’, ‘d’ , ‘e’ , ‘f’] let item1 = array.slice(2, 3) let item2 = array.slice(2, 4) let item3 = array.slice(2, 5) console.log(item1); // [‘c’] console.log(item2); // [‘c’, ‘d’] console.log(item3); // [‘c’, ‘d’ , ‘e’] | |
Math.random | 要素のkyeをランダムで取得 |
例) let array = [‘1’, ‘2’, ‘3’ , ‘4’ , ‘5’ , ‘6’]; let item = Math.floor(Math.random() * array.length); // 0~5の値をランダムで取得 console.log(array[item]); // 1から6がランダムで出力される | |
indexOf | 要素のKeyを取得:要素の文字列を入力して該当するものがあれば「先頭」の要素が配列の何番目にあるか(Key)を取得する |
例) let array = [‘a’, ‘b’, ‘c’ , ‘d’ , ‘e’ , ‘c’ , ‘e’ , ‘e’] let index = array.indexOf(‘c’) console.log(index) // 2 | |
lastIndexOf | 要素のKeyを取得:要素の文字列を入力して該当するものがあれば「最後」の要素が配列の何番目にあるか(Key)を取得する |
例) let array = [‘a’, ‘b’, ‘c’ , ‘d’ , ‘e’ , ‘c’ , ‘e’ , ‘e’] let index = array.lastIndexOf(‘c’) console.log(index) // 5 | |
Math.max.apply() | 要素の取得:一番「大きい」値の要素を取得 |
例) let array = [‘1’, ‘2’, ‘0’ , ‘3’ , ’10’ , ‘4’ , ‘5’ , ‘6’] let item = Math.max.apply(null , array) console.log(item); // 10 | |
Math.min.apply() | 要素の取得:一番「小さい」値の要素を取得 |
例) let array = [‘1’, ‘2’, ‘0’ , ‘3’ , ’10’ , ‘4’ , ‘5’ , ‘6’] let item = Math.min.apply(null , array) console.log(item); // 0 | |
unshift | 追加:先頭に追加する |
例) let array = [‘b’, ‘c’] array.unshift(‘z’) console.log(array) // [‘z’, ‘b’, ‘c’] | |
push | 追加:末尾に追加する |
例) let array = [‘a’, ‘b’] array.push(‘z’) console.log(array)// [‘a’, ‘b’, ‘z’] | |
splice | 追加:指定した位置に追加する(1つ以上追加できる) |
例) // 2番目に追加 let array = [‘a’, ‘b’, ‘c’] array.splice(1, 0, ‘Z’) console.log(array) // [‘a’, ‘Z’, ‘b’, ‘c’] 例) //3番目に2つの要素を追加 let array = [‘a’, ‘b’, ‘c’] array.splice(2, 0, ‘X’, ‘Z’) console.log(array) // [‘a’, ‘b’, ‘X’, ‘Z’, ‘c’] | |
concat() | 結合:配列を1つの配列にまとめる |
例) let array1 = [‘1’, ‘2’, ‘3’] let array2 = [‘4’, ‘5’, ‘6’] let array3 = array1.concat(array2) console.log(array3) // [“1”, “2”, “3”, “4”, “5”, “6”] | |
Array.prototype.push.apply() | 結合:2つの配列を1つにまとめる 1つ目の配列に対して2つ目の配列を結合する 2つ目の配列はそのままになる |
例) let array1 = [‘a’, ‘b’] let array2 = [‘c’, ‘d’] Array.prototype.push.apply(array1, array2) console.log(array1) // [‘a’, ‘b’, ‘c’, ‘d’] console.log(array2) // [‘c’, ‘d’] | |
shift | 削除:先頭を削除 |
例) let array = [‘a’, ‘b’, ‘c’ , ‘d’] array.shift(); console.log(array) // [‘b’, ‘c’, ‘d’] | |
void | 削除:先頭の配列の中身をundefined に置き換える |
例) let array = [‘a’, ‘b’, ‘c’] array[0] = void 0 console.log(array) // [undefined, ‘b’, ‘c’] | |
pop | 削除:末尾を削除する |
例) let array = [‘a’, ‘b’, ‘c’, ‘b’] array.pop() console.log(array); // [‘a’, ‘b’, ‘b’] | |
splice | 削除:範囲指定して要素を削除する 例えば「配列の中の1〜3番目の要素までをすべて削除」などの指定ができる |
例) let array = [‘a’, ‘b’, ‘c’, ‘d’] array.splice(1, 1) // 2番目から1つの要素を削除 console.log(array) // [‘a’, ‘c’, ‘d’] 例) let array = [‘a’, ‘b’, ‘c’, ‘d’] array.splice(1, 3); // 2番目から3つ削除 console.log(array); // [‘a’] | |
array.length = 0 | 削除:全部削除 |
例) let array = [‘a’, ‘b’, ‘c’, ‘d’] array.length = 0 console.log(array) // [] | |
array= [] | 削除:全部削除 |
例) let array = [‘a’, ‘b’, ‘c’, ‘d’] array = [] console.log(array) // [] |