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]
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Array.prototype.keys' in that specification. | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262)The definition of 'Array.prototype.keys' in that specification. | Living Standard | |
浏览器兼容性
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic Support | 38 | (Yes) | 28 | No | 25 | 7.1 |
Feature | Android | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic Support | (Yes) | (Yes) | (Yes) | 28 | No | (Yes) | 8.0 |