To iterate through array keys in JavaScript, we can use the JavaScript array keys
method to get the keys.
For instance, we can write:
const widthRange = []
widthRange[46] = {
min: 0,
max: 52
};
widthRange[66] = {
min: 52,
max: 70
};
widthRange[90] = {
min: 70,
max: 94
};
for (const i of widthRange.keys()) {
if (widthRange[i]) {
console.log(i)
}
}
to loop through the keys of the widthRange
array with the for-of loop.
We call the keys
method to remove all the indexes of the array.
And in the loop body, we check if the item widthRange[i]
is truthy before logging its value.
Therefore. we get 46, 66, and 90 from the console log.