TypedArray.prototype.keys()
TypedArray 实例的 keys() 方法会返回一个新的数组迭代器对象,其中包含类型化数组中每个索引的键。此方法与 Array.prototype.keys() 具有相同的算法。
试一试
const uint8 = new Uint8Array([10, 20, 30, 40, 50]);
const keys = uint8.keys();
keys.next();
keys.next();
console.log(keys.next().value);
// Expected output: 2
语法
js
keys()
参数
无。
返回值
一个新的可迭代迭代器对象。
描述
有关更多详细信息,请参阅 Array.prototype.keys()。此方法不是通用的,只能在类型化数组实例上调用。
示例
使用 for...of 循环进行迭代
js
const arr = new Uint8Array([10, 20, 30, 40, 50]);
const arrKeys = arr.keys();
for (const n of arrKeys) {
console.log(n);
}
备用迭代方式
js
const arr = new Uint8Array([10, 20, 30, 40, 50]);
const arrKeys = arr.keys();
console.log(arrKeys.next().value); // 0
console.log(arrKeys.next().value); // 1
console.log(arrKeys.next().value); // 2
console.log(arrKeys.next().value); // 3
console.log(arrKeys.next().value); // 4
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-%typedarray%.prototype.keys |
浏览器兼容性
加载中…