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
Description: 'Returns an iterator with key/value pairs for each index in the array.'
4
+
Subjects:
5
+
- 'Computer Science'
6
+
- 'Web Development'
7
+
Tags:
8
+
- 'Arrays'
9
+
- 'Iterators'
10
+
- 'JavaScript'
11
+
CatalogContent:
12
+
- 'learn-javascript'
13
+
- 'paths/web-development'
14
+
---
15
+
16
+
The **`entries()`** method returns a new array iterator object that contains key/value pairs for each index in the array. This is useful when both the index and value of array elements are needed during iteration.
17
+
18
+
## Syntax
19
+
20
+
```pseudo
21
+
array.entries()
22
+
```
23
+
24
+
**Parameters:**
25
+
26
+
The `entries()` method does not take any parameters.
27
+
28
+
**Return value:**
29
+
30
+
A new `Array Iterator` object containing `[index, value]` pairs
31
+
32
+
## Example
33
+
34
+
In this example the `.entries()` method is used to iterate over an array of cat names, accessing both the index and value in each loop:
35
+
36
+
```js
37
+
constcats= ['Peche', 'Moana', 'Pintassilga'];
38
+
constiterator=cats.entries();
39
+
40
+
for (let [index, name] of iterator) {
41
+
console.log(`Cat #${index}: ${name}`);
42
+
}
43
+
```
44
+
45
+
The output of this code is:
46
+
47
+
```shell
48
+
Cat #0: Peche
49
+
Cat #1: Moana
50
+
Cat #2: Pintassilga
51
+
```
52
+
53
+
## Codebyte Example
54
+
55
+
In this example the loop prints each index and corresponding fruit name by iterating through the key/value pairs returned by `.entries()`:
0 commit comments