The JavaScript Array entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.
Syntax
array.entries()
This method returns a new Array iterator object.
Example 1: Simple Array.entries() Method Example
var array1 = ['a', 'b', 'c'];
var iterator1 = array1.entries();
console.log(iterator1.next().value);
console.log(iterator1.next().value);
Output:
[0, "a"]
[1, "b"]
Example 2: Iterating with index and element
const a = ['a', 'b', 'c'];
for (const [index, element] of a.entries())
console.log(index, element);
Output:
[0, 'a']
[1, 'b']
[2, 'c']
Example 3: Using a for…of loop
var a = ['a', 'b', 'c'];
var iterator = a.entries();
for (let e of iterator) {
console.log(e);
}
Output:
[0, 'a']
[1, 'b']
[2, 'c']
Comments
Post a Comment
Leave Comment