From f149c518dff5ee004595d26bd05231eb4444d1dd Mon Sep 17 00:00:00 2001 From: Ian Sanders Date: Thu, 28 Apr 2022 10:40:56 -0400 Subject: [PATCH 1/2] Add an example for accessing the element index Add an example using `Array.prototype.entries()` to access the element index in a `for...of` loop. Resolves #216 --- docs/rules/array-foreach.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/rules/array-foreach.md b/docs/rules/array-foreach.md index 4e4da623..feba22b6 100644 --- a/docs/rules/array-foreach.md +++ b/docs/rules/array-foreach.md @@ -92,6 +92,13 @@ for (const el of els) { } ``` +Use [`entries()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries) to get access to the index: +```js +for (const [i, el] of els.entries()) { + els.name = `Element ${i}` +} +``` + ## Version 4.3.2 From 80d51fe0c7aecf55bda069695116c45dccb660dd Mon Sep 17 00:00:00 2001 From: Ian Sanders Date: Thu, 28 Apr 2022 10:45:08 -0400 Subject: [PATCH 2/2] Fix typo (`els` -> `el`) --- docs/rules/array-foreach.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/rules/array-foreach.md b/docs/rules/array-foreach.md index feba22b6..5949a10e 100644 --- a/docs/rules/array-foreach.md +++ b/docs/rules/array-foreach.md @@ -95,7 +95,7 @@ for (const el of els) { Use [`entries()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries) to get access to the index: ```js for (const [i, el] of els.entries()) { - els.name = `Element ${i}` + el.name = `Element ${i}` } ```