This technique produces a fresh Array Iterator object that holds key/value pairs corresponding to each index within the array. For every element in the initial array, the newly created iterator object will consist of an array where the index serves as the key and the element acts as the value.
What is Iterator?
An iterator is an object that maintains its current position as it sequentially accesses elements within a collection, one at a time.
An iterator provides an object that contains two attributes: key and value.
Syntax:
Example
array.entries ()
Parameters:
No parameters.
Return value:
A new Array Iterator object.
Browser Support:
| Chrome | 38 |
|---|---|
Edge |
Yes |
| Firefox | 28 |
| Opera | No |
Example 1
Example
<script type="text/javascript">
// JavaScript to illustrate entries() method
var array1 = ["Example","Core java","Advanced java"];
var iterator1 = array1.entries();
document.write(iterator1.next().value);
document.write("<br>")
// expected output: Array [0, "Example"]
document.write(iterator1.next().value);
// expected output: Array [1, "Core java"]
</script>
Output:
Output
[0, 'Example']
[1,'Core java']
Example 2
Example
<script type="text/javascript">
// JavaScript to illustrate entries() method
// Input array
var a = ['Core Java', 'Python', 'Android'];
var iterator = a.entries();
for (let e of iterator) {
document.write("<br>")
document.write(e);
}
// expected output
// [0, 'core Java']
// [1, 'Python']
// [2, 'Android']
</script>
Output:
Output
[0, 'core Java']
[1, 'Python']
[2, 'Android']