[ @@iterator ] 【イテレータ取得】
keys【イテレータ取得 (キー)】
values【イテレータ取得 (値)】
entries【イテレータ取得 (キー・値)】
at【インデックス位置要素 取得】
%TypedArray%.prototype[ @@iterator ] 【イテレータ取得】
%TypedArray%.prototype.keys【イテレータ取得 (キー)】
%TypedArray%.prototype.values【イテレータ取得 (値)】
%TypedArray%.prototype.entries【イテレータ取得 (キー・値)】
メモ
概要
- 各種イテレータオブジェクトを取得
メソッド 備考 [ @@iterator ] 【イテレータ取得】 初期値は values【イテレータ取得 (値)】
(@@iterator の実装:Symbol.iterator)keys【イテレータ取得 (キー)】 キーのイテレータオブジェクト取得 values【イテレータ取得 (値)】 値のイテレータオブジェクト取得 entries【イテレータ取得 (キー・値)】 配列 (キー・値) のイテレータオブジェクト取得
関連
外部リンク
- ECMA-262 (英語)
%TypedArray%.prototype [ @@iterator ] ( ) ES2024 (15) ES2023 (14) ES2022 (13) %TypedArray%.prototype.keys ( ) ES2024 (15) ES2023 (14) ES2022 (13) %TypedArray%.prototype.values ( ) ES2024 (15) ES2023 (14) ES2022 (13) %TypedArray%.prototype.entries ( ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
typedArray[ Symbol.iterator ]()
イテレータオブジェクト
for (const value of typedArray ) {
value の処理
}
typedArray.keys()
イテレータオブジェクト (キー)
typedArray.values()
イテレータオブジェクト (値)
typedArray.entries()
イテレータオブジェクト (キー と 値 の配列)
例
const array = [ 1, -2, 3, -4, 5 ];
const typedArray = new Int32Array(array);
console.log(typedArray);
// 出力:Int32Array(5) [ 1, -2, 3, -4, 5 ]
const iter = typedArray[Symbol.iterator]();
for (const value of iter) {
console.log(value);
}
// 出力:
// 1
// -2
// 3
// -4
// 5
const iter2 = typedArray[Symbol.iterator]();
let result = iter2.next();
while (! result.done) {
console.log(result.value);
result = iter2.next();
}
// 出力:
// 1
// -2
// 3
// -4
// 5
for (const value of typedArray) {
console.log(value);
}
// 出力:
// 1
// -2
// 3
// -4
// 5
const array = [ 1, -2, 3, -4, 5 ];
const typedArray = new Int32Array(array);
const iter = typedArray.keys();
for (const key of iter) {
console.log(key);
}
// 出力:
// 0
// 1
// 2
// 3
// 4
const iter2 = typedArray.keys();
let result = iter2.next();
while (! result.done) {
console.log(result.value);
result = iter2.next();
}
// 出力:
// 0
// 1
// 2
// 3
// 4
const array = [ 1, -2, 3, -4, 5 ];
const typedArray = new Int32Array(array);
const iter = typedArray.values();
for (const value of iter) {
console.log(value);
}
// 出力:
// 1
// -2
// 3
// -4
// 5
const iter2 = typedArray.values();
let result = iter2.next();
while (! result.done) {
console.log(result.value);
result = iter2.next();
}
// 出力:
// 1
// -2
// 3
// -4
// 5
const array = [ 1, -2, 3, -4, 5 ];
const typedArray = new Int32Array(array);
const iter = typedArray.entries();
for (const entry of iter) {
console.log(entry);
}
// 出力:
// Array [ 0, 1 ]
// Array [ 1, -2 ]
// Array [ 2, 3 ]
// Array [ 3, -4 ]
// Array [ 4, 5 ]
const iter2 = typedArray.entries();
for (const [key, value] of iter2) {
console.log(key, value);
}
// 出力:
// 0 1
// 1 -2
// 2 3
// 3 -4
// 4 5
const iter3 = typedArray.entries();
let result = iter3.next();
while (! result.done) {
console.log(result.value);
result = iter3.next();
}
// 出力:
// Array [ 0, 1 ]
// Array [ 1, -2 ]
// Array [ 2, 3 ]
// Array [ 3, -4 ]
// Array [ 4, 5 ]
const iter4 = typedArray.entries();
let result4 = iter4.next();
while (! result4.done) {
const [key, value] = result4.value;
console.log(key, value);
result4 = iter4.next();
}
// 出力:
// 0 1
// 1 -2
// 2 3
// 3 -4
// 4 5
%TypedArray%.prototype.at【インデックス位置要素 取得】
メモ
概要
- インデックス位置の要素を取得
- [ ]【要素アクセス】でも可能 (負数不可)
- 負数指定も可 (末尾から)
関連
外部リンク
- ECMA-262 (英語)
%TypedArray%.prototype.at ( index ) ES2022 (14) ES2022 (14) ES2022 (13)
構文
typedArray.at( index )
インデックス位置の要素 (undefined:範囲外)
index インデックス位置
0 ≦先頭から
< 0末尾から
例
const array = [ 1, -2, 3, -4, 5 ];
const typedArray = new Int32Array(array);
for (let i = 0; i < typedArray.length; i++) {
console.log(i, typedArray[i], typedArray.at(i));
}
// 出力:
// 0 1 1
// 1 -2 -2
// 2 3 3
// 3 -4 -4
// 4 5 5
for (let i = 1; i <= typedArray.length; i++) {
console.log(-i, typedArray[-i], typedArray.at(-i));
}
// 出力:
// -1 undefined 5
// -2 undefined -4
// -3 undefined 3
// -4 undefined -2
// -5 undefined 1
// 範囲外
console.log(typedArray[5], typedArray.at(5));
// 出力:undefined undefined
console.log(typedArray[-1], typedArray.at(-6));
// 出力:undefined undefined