[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/keys [Back]  [Original]

Array.prototype.keys() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Array.prototype.keys()

20157

keys()

const array1 = ["a", "b", "c"];
const iterator = array1.keys();

for (const key of iterator) {
  console.log(key);
}

// Expected output: 0
// Expected output: 1
// Expected output: 2

js
keys()

keys() undefined

keys() this length

keys()

Object.keys() keys()

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

keys()

keys() this length 0 length - 1

js
const arrayLike = {
  length: 3,
};
for (const entry of Array.prototype.keys.call(arrayLike)) {
  console.log(entry);
}
// 0
// 1
// 2

ECMAScript 2027 LanguageSpecification
# sec-array.prototype.keys


Web Proxy Viewer  |  New URL  |  Original Page