KEMBAR78
Add an example for accessing the element index in a `for...of` loop by iansan5653 · Pull Request #241 · github/eslint-plugin-github · GitHub
Skip to content

Conversation

@iansan5653
Copy link
Member

@iansan5653 iansan5653 commented Apr 28, 2022

Add an example using Array.prototype.entries() to access the element index in a for...of loop.

for (const [i, el] of els.entries()) {
  el.name = `Element ${i}`
}

This is preferred over for...in because for...in does not guarantee that elements will be iterated over in the expected order (doesn't matter in the above example but it could in other cases).

And it's more readable than a standard for loop:

for (let i = 0; i < els.length; i++) {
  els[i].name = `Element ${i}`
}

Resolves #216

Add an example using `Array.prototype.entries()` to access the element index in a `for...of` loop.

Resolves #216
@iansan5653 iansan5653 requested a review from a team as a code owner April 28, 2022 14:44
@iansan5653 iansan5653 requested a review from koddsson April 28, 2022 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide an example alternative for Array.forEach with index

4 participants