The entries method generates a fresh iterator object from an array, which contains the key/value pairs corresponding to each element within the array. Here, a key signifies the index number associated with its respective value. This operation does not alter the original array.
Syntax
The syntax displayed below illustrates the entries function:
array.entries()
Parameters
It does not have any parameters.
Return
It produces a newly instantiated array iterator object. This iterator object symbolizes each element within the array, with corresponding keys assigned to each element.
JavaScript Array entries Method Examples
Let us explore a few examples to gain a clearer understanding of the toString method:
Example 1: A basic usage of the entries method for an array.
<html>
<head> <h5> Array Methods </h5>
<body>
<script>
var arr=['John','Michael','Embrose','Herry','Lewis'];
var itr=arr.entries();
document.write("After applying the entries method:"+"<br>");
for(var e of itr) //for loop using var.
{
document.write(e+"</br>");
}
</script>
</body>
</head>
</html>
Output:
The output displays the components of the array, each associated with a specific key. Collectively, these elements constitute a key/value pair.
Example2: This illustration demonstrates the use of the array entries method utilizing the let declaration.
<html>
<head> <h5> Array Methods </h5>
<body>
<script>
var arr=['John','Michael','Embrose','Herry','Lewis']; // array elements
var itr=arr.entries();
document.write("After applying the entries method:"+"<br>");
for(let e of itr) // let declares a variable, but its scope is within the block.
{
document.write(e+"</br>");
}
</script>
</body>
</head>
</html>
Output:
The result will remain identical to what is illustrated in the prior example.
Example3: In this illustration, the (key,value) pairs contained within the array are showcased upon clicking the "apply" button.
<html>
<head> <h5> Array Methods </h5>
<body>
<script>
function arr()
{
var name=['John','Serious','Hadrik','Harry'];
var itr=name.entries(); //Using entries() method.
document.write("After applying the entries method:"+"<br>");
for(x of itr)
{
document.write("<br>"+x); //This will display one array element per line.
}
}
</script>
</body>
<input type="button" onClick="arr()" value=" Apply"/> //invoking the arr() function.
</head>
</html>
Output:
Upon invoking the arr function, the resulting output is:
Example4: Showing the rankings of the candidates within the specified array.
<html>
<head> <h5> Array Methods </h5>
<body>
<script>
function position()
{
var name=['John','Serious','Hadrik','Harry'];
var itr=name.entries(); //Using entries() method.
document.write("The positions of each candidate in a sequence are: "+"<br>");
for(x of itr)
{
document.write("<br>"+x); //This will display one array element per line with its key.
}
}
</script>
</body>
<input type="button" onClick="position()" value=" Click to Display"/>
</head>
</html>
Output:
The output will be:
Example5:
<html>
<head> <h5> Array Methods </h5>
<body>
<script>
function position()
{
var name=[1,2,3,4,5,6,7,8,8,9];
var itr=name.entries(); //Using entries() method.
document.write("The positions of each number in a sequence are: "+"<br>");
for(x of itr)
{
document.write("<br>"+x); //This will display one array element per line with its key.
}
}
</script>
</body>
<input type="button" onClick="position()" value=" Click to Display"/>
</head>
</html>
Output:
Upon selecting the "display" button, the arrangement of each value within the array will be as follows: