What is array entries() in Javascript?
Overview
The entries function in JavaScript is used to return an array iterator object. entries can print each of the indices and the values present on that index in the array. The general syntax for the entries function is:
array.shift()
Parameters
The entries function takes no mandatory inputs as arguments.
Return value
The entries function returns an object of type array iterator.
Example
The following example will help you understand the entries function and how you can use it.
First, we create the array and call the entries function on it to acquire the array iterator object. Then, we can use the following methods to print the indices and values on those indices.
Using the next().value
You can use the next().value function to acquire the value present at the next index. Each print using console.log will print the next element in the array. If you iterate beyond the range of the array, it prints undefined, as the array is not defined for that index.
Using the for loop
You can also use the for loop method to print each index and its element.
const arr1 = ['1', '2', '3'];const i1 = arr1.entries();//method1console.log('method1')console.log(i1.next().value);console.log(i1.next().value);console.log(i1.next().value);console.log(i1.next().value);//method2console.log('method2')const i2 = arr1.entries();for (let item of i2) {console.log(item);}
Free Resources