Javascript Array keys()

Introduction

Array keys() will return an iterator of the array's indices.

arr.keys()
const a = ["foo", "bar", "baz", "qux"];
const aKeys = Array.from(a.keys());
console.log(aKeys);/*from www  .j a  v a2s .  co  m*/

Create an Array Iterator object containing the keys of the array:


var languages = ["CSS", "HTML", "Java", "Javascript"];
var fk = languages.keys();

for (x of fk) {/*  ww  w  .ja  v a 2s . c  o  m*/
  console.log(x);
}

Key iterator doesn't ignore holes

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



PreviousNext

Related