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
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
keysChrome Full support 38Edge Full support 12Firefox Full support 28IE No support NoOpera Full support 25Safari Full support 8WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 28Opera Android Full support YesSafari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support 0.12

Legend

Full support  
Full support
No support  
No support

相关链接