Array.prototype.keys()

 keys() 方法返回一个包含数组中每个索引键的Array Iterator对象。

语法

arr.keys()

返回值 

一个新的 Array 迭代器对象。

示例

索引迭代器会包含那些没有对应元素的索引

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 Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
Array.prototype.keys
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
Array.prototype.keys
Draft  

浏览器兼容性

Update compatibility data on GitHub
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
keys Chrome Full support 38 Edge Full support 12 Firefox Full support 28 IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support Yes Chrome Android Full support 38 Firefox Android Full support 28 Opera Android Full support Yes Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support 0.12

Legend

Full support  
Full support
No support  
No support

相关链接