Array.prototype.keys()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since May 2018.

keys() 方法會回傳一個包含陣列中的每一個索引之鍵(keys)的新 Array Iterator 物件。

嘗試一下

語法

arr.keys()

回傳值

一個新的 Array 迭代器(iterator)物件。

範例

鍵迭代器不會乎略陣列中的空元素

js
var arr = ["a", , "c"];
var sparseKeys = Object.keys(arr);
var denseKeys = [...arr.keys()];
console.log(sparseKeys); // ['0', '2']
console.log(denseKeys); // [0, 1, 2]

規範

Specification
ECMAScript Language Specification
# sec-array.prototype.keys

瀏覽器相容性

BCD tables only load in the browser

參見