Skip to content

Commit 188d3cf

Browse files
authored
Add entry for JavaScript Array.prototype.entries() (#7115)
* Add entry for JavaScript Array.prototype.entries() * content fixes * Update entries.md ---------
1 parent c21fdb4 commit 188d3cf

File tree

1 file changed

+64
-0
lines changed
  • content/javascript/concepts/arrays/terms/entries

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
Title: '.entries()'
3+
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+
const cats = ['Peche', 'Moana', 'Pintassilga'];
38+
const iterator = 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()`:
56+
57+
```codebyte/javascript
58+
const fruits = ['apple', 'banana', 'cherry'];
59+
const iterator = fruits.entries();
60+
61+
for (let [index, fruit] of iterator) {
62+
console.log(index, fruit);
63+
}
64+
```

0 commit comments

Comments
 (0)