Iterating over Array Indexes
Iterating over Array Indexes with keys()
In JavaScript, the keys()
method allows you to create an iterator object that contains the indexes (or keys) of the elements in an array.
The keys()
method does not change the original array.
Examples
Example 1, using keys()
to iterate over an array:
Example 2, using keys()
with a sparse array:
If an array has βholesβ (i.e. missing elements), the keys()
method still iterates over all possible indexes.