You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: questions/how-do-you-access-the-index-of-an-element-in-an-array-during-iteration/en-US.mdx
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ title: How do you access the index of an element in an array during iteration?
6
6
7
7
To access the index of an element in an array during iteration, you can use methods like `forEach`, `map`, `for...of` with `entries`, or a traditional `for` loop. For example, using `forEach`:
The `forEach` method executes a provided function once for each array element. The callback function takes three arguments: the current element, the index of the current element, and the array itself.
The `map` method creates a new array populated with the results of calling a provided function on every element in the calling array. The callback function also takes three arguments: the current element, the index of the current element, and the array itself.
32
32
33
-
```js
33
+
```js live
34
34
constarray= ['a', 'b', 'c'];
35
35
array.map((element, index) => {
36
36
console.log(index, element);
@@ -41,7 +41,7 @@ array.map((element, index) => {
41
41
42
42
The `for...of` loop can be combined with the `entries` method to access both the index and the element.
43
43
44
-
```js
44
+
```js live
45
45
constarray= ['a', 'b', 'c'];
46
46
for (const [index, element] ofarray.entries()) {
47
47
console.log(index, element);
@@ -52,7 +52,7 @@ for (const [index, element] of array.entries()) {
52
52
53
53
A traditional `for` loop gives you direct access to the index.
54
54
55
-
```js
55
+
```js live
56
56
constarray= ['a', 'b', 'c'];
57
57
for (let index =0; index <array.length; index++) {
0 commit comments