This article will cover all the facts in detail which are related to the method, namely Array.entries() under Arrays in JavaScript.
The Array.entries() method works on iterable objects such as an array (or any data iterable data structure) and it is used to fetch all the entries of the same data structure. This method is used to get a new Array that contains the key and value pairs for each index of an array.
Syntax:
array.entries()
Parameters: This method does not accept any parameter.
Return value: It returns an array of indexes and values of the given array on which the Array.entries() method is going to work.
The below examples illustrate the JavaScript Array entries() Method
Example 1: In this example, we will see the basic use of the Javascript Array.entries() method.
Javascript
let languages = [ "HTML" , "CSS" , "JavaScript" , "ReactJS" ];
let g = languages.entries();
for (x of g) {
console.log( "geeks" ,x);
}
|
Outputgeeks [ 0, 'HTML' ]
geeks [ 1, 'CSS' ]
geeks [ 2, 'JavaScript' ]
geeks [ 3, 'ReactJS' ]
Example 2: In this example, we will see the use of the Javascript Array.entries() method.
JavaScript
let arr = [ "HTML" , "CSS" , "JS" ,
"Bootstrap" , "PHP" ];
let seqn = arr.entries();
console.log( "Applying the Array entries method:" );
for (let x of seqn) {
console.log(x);
};
|
Output:
Applying the Array entries method:
0,HTML
1,CSS
2,JS
3,Bootstrap
4,PHP
Example 3:
JavaScript
const x = [ "Geeks" , "for" , "Geeks" ];
for (const [index, element] of x.entries())
console.log(index, element);
|
Output:
0 'Geeks'
1 'for'
2 'Geeks'
Example 4: In this example, we will understand the use of the above-illustrated method in a somehow different way using several other methods which is next().
Javascript
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);
|
Output:
[ 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:
- Google Chrome 38.0
- Microsoft Edge 12.0
- Mozilla Firefox 28.0
- Safari 8.0
- Opera 25.0
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.