Skip to content

Commit 52b9eb3

Browse files
committed
exec: how-do-you-access-the-index-of-an-element-in-an-array-during-iteration
1 parent 93fc6c7 commit 52b9eb3

File tree

1 file changed

+5
-5
lines changed
  • questions/how-do-you-access-the-index-of-an-element-in-an-array-during-iteration

1 file changed

+5
-5
lines changed

questions/how-do-you-access-the-index-of-an-element-in-an-array-during-iteration/en-US.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: How do you access the index of an element in an array during iteration?
66

77
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`:
88

9-
```js
9+
```js live
1010
const array = ['a', 'b', 'c'];
1111
array.forEach((element, index) => {
1212
console.log(index, element);
@@ -19,7 +19,7 @@ array.forEach((element, index) => {
1919

2020
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.
2121

22-
```js
22+
```js live
2323
const array = ['a', 'b', 'c'];
2424
array.forEach((element, index) => {
2525
console.log(index, element);
@@ -30,7 +30,7 @@ array.forEach((element, index) => {
3030

3131
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.
3232

33-
```js
33+
```js live
3434
const array = ['a', 'b', 'c'];
3535
array.map((element, index) => {
3636
console.log(index, element);
@@ -41,7 +41,7 @@ array.map((element, index) => {
4141

4242
The `for...of` loop can be combined with the `entries` method to access both the index and the element.
4343

44-
```js
44+
```js live
4545
const array = ['a', 'b', 'c'];
4646
for (const [index, element] of array.entries()) {
4747
console.log(index, element);
@@ -52,7 +52,7 @@ for (const [index, element] of array.entries()) {
5252

5353
A traditional `for` loop gives you direct access to the index.
5454

55-
```js
55+
```js live
5656
const array = ['a', 'b', 'c'];
5757
for (let index = 0; index < array.length; index++) {
5858
console.log(index, array[index]);

0 commit comments

Comments
 (0)