Skip to content

Instantly share code, notes, and snippets.

@aderaaij
Last active November 14, 2017 22:18
Show Gist options
  • Save aderaaij/406dd6ada20de5cad2baaa61179d896d to your computer and use it in GitHub Desktop.
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.
// 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