JavaScript Array entries()
Example
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// Create an Iterator
const list = fruits.entries();
// List the Entries
let text = "";
for (let x of list) {
text += x;
}
Try it Yourself »
More Examples Below!
Description
The entries()
method returns an Iterator object with the key/value pairs from an array:
[0, "Banana"]
[1, "Orange"]
[2, "Apple"]
[3, "Mango"]
The entries()
method does not change the original array.
Array Iteration Methods:
Syntax
array.entries()
Parameters
NONE |
Return Value
Type | Description |
Iterable | An Iterable object with the key/value pairs from the array. |
More Examples
Example
Iterate directly over the Iterator:
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// List the Entries
let text = "";
for (let x of fruits.entries()) {
text += x;
}
Try it Yourself »
Example
Use the built in Object.entries() method:
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// List the Entries
let text = "";
for (let x of Object.entries(fruits)) {
text += x;
}
Try it Yourself »
Note
It is not a good practice to save an iterator.
The iterator has a next() method to access each element one at a time.
As soon as you start using it, it cannot be reset or restarted.
Example
Use the next() method of the iterator:
// Create an Array
const fruits = ["Banana", "Orange", "Apple", "Mango"];
// Create an Interator
const list = fruits.entries();
let text = list.next().value + " " + list.next().value;
Try it Yourself »
Array Tutorials:
Browser Support
entries()
is an ECMAScript6 (ES6) feature.
ES6 (JavaScript 2015) is supported in all modern browsers since June 2017:
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |
entries()
is not supported in Internet Explorer.