Sometimes, we want to iterate array keys in JavaScript.
In this article, we’ll look at how to iterate array keys in JavaScript.
How to iterate array keys in JavaScript?
To iterate array keys in JavaScript, we can get the keys with the keys
method.
For instance, we write
for (const k of Array.from(Array(10).keys())) {
console.log(k);
}
to get the keys of the Array(10)
array as an array of keys with Array.from(Array(10).keys())
.
Then we loop through each of them with a for-of loop.
Conclusion
To iterate array keys in JavaScript, we can get the keys with the keys
method.