Last Updated : 15 Jul, 2024
The entries()
method in JavaScript is used to create an iterator that returns key/value pairs for each index in the array.
It allows iterating over arrays and accessing both the index and value of each element sequentially.
Syntax:array.entries()Parameters:
Example 1: Iterating Array Entries in JavaScript
The code initializes an array languages
and creates an iterator g
using the entries()
method. It then iterates over each key/value pair in the iterator using a for...of
loop, logging each pair along with the string "geeks" to the console.
let languages = ["HTML", "CSS", "JavaScript", "ReactJS"];
let g = languages.entries();
for (x of g) {
console.log("geeks",x);
}
geeks [ 0, 'HTML' ] geeks [ 1, 'CSS' ] geeks [ 2, 'JavaScript' ] geeks [ 3, 'ReactJS' ]
Example 2: Iterating Over Array Entries in JavaScript
The code initializes an array fruits_names
containing fruit names. It then creates an iterator fruits_array_iterator
using the entries()
method. Two calls to next().value
are made on the iterator, logging the key/value pairs (index and corresponding element) to the console.
let fruits_names = ['apple', 'banana', 'mango'];
let fruits_array_iterator = fruits_names.entries();
console.log(fruits_array_iterator.next().value);
console.log(fruits_array_iterator.next().value);
[ 0, 'apple' ] [ 1, 'banana' ]
We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.
Supported Browsers:The browsers supported by the JavaScript Array entries() method are listed below:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4