This JavaScript tutorial explains how to use the Array technique known as entries() with syntax and examples.
Description
In JavaScript, entries() is an Array method that is used to return a new Array iterator object that approves you to iterate via the key/value pairs in the array. Because the entries() method is a method of the Array object, it ought to be invoked thru a specific occasion of the Array class.
Syntax
In JavaScript, the syntax for the entries() technique is:
array.entries();
Parameters or Arguments
There are no parameters or arguments for the entries() method.
Returns
The entries() approach returns a new Array iterator object that lets in you to iterate through the key/value pairs in the array. The Array iterator object has a built-in technique referred to as next() that can be used to get the subsequent value.
Example
Let’s take a appear at an example of how to use the entries() method in JavaScript.
For example:
var totn_array = ['Tech','On','The','Net'];
var totn_iterator = totn_array.entries();
console.log(totn_iterator.next().value);
console.log(totn_iterator.next().value);
console.log(totn_iterator.next().value);
console.log(totn_iterator.next().value);
In this example, we have declared an array object referred to as totn_array that has four elements. We have then invoked the entries() approach of the totn_array variable to create an Array iterator object called totn_iterator.
We have written the output of the next().value methods of the Array iterator to the net browser console log, for demonstration purposes, to show what these calls return.
The following will be output to the web browser console log:
[0, "Tech"]
[1, "On"]
[2, "The"]
[3, "Net"]
In this example, every of the calls will return the subsequent entry in the Array iterator object. And if we referred to as the next().value technique one greater time (where there are no greater entries), JavaScript would return a cost of undefined.
undefined
Using a FOR Loop
Instead of using the next() approach of the Array iterator object, you can additionally a for loop to iterator thru the factors of the array.
For example:
var totn_array = ['Tech','On','The','Net'];
for (const [index, element] of totn_array.entries())
console.log(element);
The following will be output to the internet browser console log:
"Tech"
"On"
"The"
"Net"
In this example, the for loop will output each component in the array. We use the const key-word to ensure that the elements of the array are not modified while looping via the for loop.
If you desired to doubtlessly trade the array factors while looping, you would remove the const keyword.
For example:
var totn_array = ['Tech','On','The','Net'];
for ([index, element] of totn_array.entries())
{
if (index == 0) element = "totn";
console.log(element);
}
The following will be output to the web browser console log:
"totn"
"On"
"The"
"Net"
In this example, we are putting the first element in the array (where index is equal to 0) to “totn” whilst we are iterating through the elements. Therefore, we have not noted the const keyword in the for loop. Otherwise, we would have raised an “Uncaught TypeError: Assignment to steady variable” error.
Leave a Review