Last active
November 14, 2017 22:18
-
-
Save aderaaij/406dd6ada20de5cad2baaa61179d896d to your computer and use it in GitHub Desktop.
A way to get a for loop with an index. For this we use Array.entries which returns an `ArrayIterable` which we can go over with array.entries().next(); Each time you call `next()` it will return an object with a `done:` status which has a boolean value and an array of the current array item and its index.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In the for loop we directly destructure the array returned by heroes.entries(); | |
for (const[i, hero] of heroes.entries()) { | |
console.log(`${hero} is hero #${i + 1}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment