The entries() method returns a new Array Iterator object containing key/value pairs for each array index.
Example
entries() Syntax
The syntax of the entries() method is:
arr.entries()
Here, arr is an array.
entries() Parameters
The entries() method does not take any parameters.
entries() Return Value
- Returns a new
Arrayiterator object.
Note: The entries() method does not change the original array.
Example 1: Using entries() Method
Output
[ 0, 'Java' ] [ 1, 'C' ] [ 2, 'C++' ] [ 3, 'Python' ]
In the above example, we have used the entries() method to get an Array iterator object of the key/value pair of each index in the language array.
We have then looped through iterator that prints the key/value pairs of each index.
Example 2: Using next() Method in Array Iterator Object
Array Iterator object has a built-in method called next() which is used to get the next value in the object.
Instead of looping through the iterator, we can get the key/value pairs using next().value. For example:
Output
[ 0, '#' ] [ 1, '$' ] [ 2, '*' ]
Recommended Reading: JavaScript Array keys()