Sometimes, we want to add the JavaScript equivalent of Python ‘enumerate’ for a sequence.
In this article, we’ll look at how to add the JavaScript equivalent of Python ‘enumerate’ for a sequence.
How to add the JavaScript equivalent of Python ‘enumerate’ for a sequence?
To add the JavaScript equivalent of Python ‘enumerate’ for a sequence, we call the array entries method.
For instance, we write
const foobar = ["A", "B", "C"];
for (const [index, element] of foobar.entries()) {
console.log(index, element);
}
to call foobar.entries to return an array with arrays of the index and element value for each entry.
index has the array entry index.
And element has the element at the index.
Conclusion
To add the JavaScript equivalent of Python ‘enumerate’ for a sequence, we call the array entries method.