在线文档教程

array.keys

array.keys

keys() 方法返回一个新的Array迭代器,它包含数组中每个索引的键。

var arr = ['a', 'b', 'c']; var iterator = arr.keys( console.log(iterator.next() // { value: 0, done: false } console.log(iterator.next() // { value: 1, done: false } console.log(iterator.next() // { value: 2, done: false } console.log(iterator.next() // { value: undefined, done: true }

语法

arr.keys()

返回值

一个新的Array迭代器对象。

示例

key迭代器不会忽略空洞

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

规范

SpecificationStatusComment
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Array.prototype.keys' in that specification.StandardInitial definition.
ECMAScript Latest Draft (ECMA-262)The definition of 'Array.prototype.keys' in that specification.Living Standard

浏览器兼容性

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic Support38(Yes)28No257.1

FeatureAndroidChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic Support(Yes)(Yes)(Yes)28No(Yes)8.0