Array【配列】オブジェクト
メモ (外部リンク) 配列リテラル・コンストラクタ プロパティ メソッド一覧 例
メモ
- 配列を表すオブジェクト
- 参考:その他 配列・コレクション
外部リンク
ECMAScript (英語)
Array Objects | ||
---|---|---|
ES2022 (13) | ES2021 (12) | ES2020 (11) |
get Array [ @@species ] | ||
ES2022 (13) | ES2021 (12) | ES2020 (11) |
Array.prototype [ @@unscopables ] | ||
ES2022 (13) | ES2021 (12) | ES2020 (11) |
Property Accessors | ||
ES2022 (13) | ES2021 (12) | ES2020 (11) |
Array.prototype.constructor | ||
ES2022 (13) | ES2021 (12) | ES2020 (11) |
length | ||
ES2022 (13) | ES2021 (12) | ES2020 (11) |
Array.prototype | ||
ES2022 (13) | ES2021 (12) | ES2020 (11) |
配列リテラル・コンストラクタ
構文 | 説明 |
---|---|
(1) [ [ item1 ] , [ item2 ] , [ … ] ] (2) [ new ] Array( len ) (3) [ new ] Array( ...items ) | 配列リテラル・コンストラクタ (1) リテラル 指定 (2) 配列要素数 指定 (3) 配列要素 指定 |
プロパティ
プロパティ | 説明 | 備考 | |
---|---|---|---|
Array | [ @@species ] | コンストラクタ定義 (下記 例 参照) | [Symbol.species] |
Array.prototype | [ @@unscopables ] | with ステートメント の除外プロパティ (下記 例 参照) | [Symbol.unscopables] ES2015 (6) 以降に追加されたプロパティ名と除外フラグ |
Array.prototype. | [ index ] | 配列の要素 (index:0~) (下記 例 参照) | 要素数以上の要素設定で、配列の拡大 |
Array.prototype. | constructor | コンストラクタ定義 (下記 例 参照) | |
length | 配列の要素数 (下記 例 参照) | 値設定で、配列の拡大・縮小 符号なし32ビット整数 (0 ~ 232-1) 範囲外の場合、RangeError 例外 | |
Array. | prototype | プロトタイプ (下記 例 参照) |
メソッド一覧
メソッド | 説明 | 元の配列の変更 | 配列生成 | |
---|---|---|---|---|
Array.prototype | [ @@iterator ]( ) | イテレータ取得 | ― | ― |
Array.prototype. | at ( index ) | インデックス位置要素 取得 | ― | ― |
Array.prototype. | concat( [ item1 [ , item2 [ , … ] ] ] ) | 連結 | ― | ● |
Array.prototype. | copyWithin( target, start [ , end ] ) | 内部コピー | あり | ― |
Array.prototype. | entries( ) | イテレータ取得 (キー・値) | ― | ― |
Array.prototype. | every( callbackfn [ , thisArg ] ) | 有効判定 (全要素有効) | コールバック関数による | ― |
Array.prototype. | fill( value [ , start [ , end ] ]) | 指定値設定 | あり | ― |
Array.prototype. | filter( callbackfn [ , thisArg ] ) | 抽出 | コールバック関数による | ● |
Array.prototype. | find( predicate [ , thisArg ] ) | 値検索 (ユーザ関数) | ― | ― |
Array.prototype. | findIndex( predicate [ , thisArg ] ) | インデックス検索 (ユーザ関数) | ― | ― |
Array.prototype. | flat( [ depth ] ) | 配列生成 (深さ指定) | ― | ● |
Array.prototype. | flatMap( mapperFunction [ , thisArg ] ) | 配列生成 (関数指定) | コールバック関数による | ● |
Array.prototype. | forEach( callbackfn [ , thisArg ] ) | 要素処理 | コールバック関数による | ― |
Array. | from( items [ , mapfn [ , thisArg ] ] ) | 配列生成 (オブジェクト指定) | ― | ● |
Array.prototype. | includes( searchElement [ , fromIndex ] ) | 存在確認 | ― | ― |
Array.prototype. | indexOf( searchElement [ , fromIndex ] ) | 検索 (順方向) | ― | ― |
Array. | isArray( arg ) | 配列判定 | ― | ― |
Array.prototype. | join( [separator] ) | 全要素文字列結合 | ― | ― |
Array.prototype. | keys( ) | イテレータ取得 (キー) | ― | ― |
Array.prototype. | lastIndexOf( searchElement [ , fromIndex ] ) | 検索 (逆方向) | ― | ― |
Array.prototype. | map( callbackfn [ , thisArg ] ) | 配列変換生成 | コールバック関数による | ● |
Array. | of( ...items ) | 配列生成 (要素指定) | ― | ● |
Array.prototype. | pop() | 末尾要素削除 | あり | ― |
Array.prototype. | push() | 末尾要素追加 | あり | ― |
Array.prototype. | reduce( callbackfn [ , initialValue ] ) | 隣接要素処理 (順方向) | コールバック関数による | ― |
Array.prototype. | reduceRight( callbackfn [ , initialValue ] ) | 隣接要素処理 (逆方向) | コールバック関数による | ― |
Array.prototype. | reverse() | 要素反転 | あり | ― |
Array.prototype. | shift() | 先頭要素削除 | あり | ― |
Array.prototype. | slice( start [, end] ) | 部分コピー | ― | ● |
Array.prototype. | some( callbackfn [ , thisArg ] ) | 有効判定 (一部要素有効) | コールバック関数による | ― |
Array.prototype. | sort( [comparefn] ) | ソート | あり | ― |
Array.prototype. | splice( start, deleteCount [ , item1 [ , item2 [ , … ] ] ] ) | 要素削除&挿入 | あり | ― |
Array.prototype. | toLocaleString() | 文字列変換 (ロケール) | ― | ― |
Array.prototype. | toString() | 文字列変換 | ― | ― |
Array.prototype. | unshift( [ item1 [ , item2 [ , … ] ] ] ) | 先頭要素挿入 | あり | ― |
Array.prototype. | values() | イテレータ取得 (値) | ― | ― |
例
// 【プロパティ】
console.log(Array[Symbol.species]);
// 出力:function Array() { [native code] }
console.log(Array.prototype);
// 出力:Array[0]
// 出力: concat: concat()
// 出力: constructor: Array()
// 出力: copyWithin: copyWithin()
// 出力: entries: entries()
// 出力: 省略
// 出力: toLocaleString: toLocaleString()
// 出力: toString: toString()
// 出力: unshift: unshift()
// 出力: Symbol(Symbol.iterator): values()
// 出力: Symbol(Symbol.unscopables): Object
// 出力: __proto__: Object
let array = [ 1, 2, 3 ];
console.log(array.constructor);
// 出力:function Array() { [native code] }
console.log(array.length);
// 出力:3
array[9] = 10;
console.log(array.length, array);
// 出力:10 [1, 2, 3, 9: 10] 配列の拡大
array.length = 20;
console.log(array.length, array);
// 出力:20 [1, 2, 3, 9: 10] 配列の拡大
array.length = 3;
console.log(array.length, array);
// 出力:3 [1, 2, 3] 配列の縮小
console.log(Array.prototype[Symbol.unscopables]);
// 出力:Object {
// 出力:copyWithin: true,
// 出力:entries: true,
// 出力:fill: true,
// 出力:find: true,
// 出力:findIndex: true,
// 出力:flat: true,
// 出力:flatMap: true,
// 出力:includes: true,
// 出力:keys: true,
// 出力:values: true
// 出力:}
let keys = ["KeyA"];
with (Array.prototype) {
keys.push("KeyB"); // 配列参照
}
console.log(keys);
// 出力:Array [ "KeyA", "KeyB" ]
Array.prototype[Symbol.unscopables]['keys'] = false;
console.log(Array.prototype[Symbol.unscopables]);
// 出力:Object {
// 出力:copyWithin: true,
// 出力:entries: true,
// 出力:fill: true,
// 出力:find: true,
// 出力:findIndex: true,
// 出力:flat: true,
// 出力:flatMap: true,
// 出力:includes: true,
// 出力:keys: false,
// 出力:values: true
// 出力:}
try {
with (Array.prototype) {
keys.push("KeyC"); // メソッド参照
}
}
catch (error) {
console.log(error);
// 出力:TypeError: "values.push is not a function"
}
console.log(keys);
// 出力:Array [ "KeyA", "KeyB" ]
// 【多次元配列】
var array = [ [1, 10], [2, 20], [3, 30] ];
console.log(array.length);
// 出力:3
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
console.log(("[" + i + "][" + j + "] = "), array[i][j]);
}
}
// 出力:[0][0] = 1
// 出力:[0][1] = 10
// 出力:[1][0] = 2
// 出力:[1][1] = 20
// 出力:[2][0] = 3
// 出力:[2][1] = 30
// 【ジャグ配列】
var array = [ [1, 10], [2, 20, 30], [3, 30, 40, 50] ];
console.log(array.length);
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
console.log(("[" + i + "][" + j + "] = "), array[i][j]);
}
}
// 出力:[0][0] = 1
// 出力:[0][1] = 10
// 出力:[1][0] = 2
// 出力:[1][1] = 20
// 出力:[1][2] = 30
// 出力:[2][0] = 3
// 出力:[2][1] = 30
// 出力:[2][2] = 40
// 出力:[2][3] = 50