Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example for accessing the element index in a for...of loop #241

Merged
merged 2 commits into from May 2, 2022

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
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
2 participants