JavaScript Array entries() Method
Below is the example of the Array entries() method.
- Example:
<!DOCTYPE html>
<
html
>
<
body
style
=
"text-align: center;"
>
<
h1
style
=
"color: green;"
>
GeeksforGeeks
</
h1
>
<
b
>
JavaScript | Array entries() Method
</
b
>
<
p
id
=
"geeks"
></
p
>
<
script
>
var lnaguages = ["HTML", "CSS",
"JavaScript", "ReactJS"];
var g = lnaguages.entries();
for (x of g) {
document.getElementById("geeks")
.innerHTML += x + "<
br
>";
}
</
script
>
</
body
>
</
html
>
- Output:
The arr.entries() method is used to get a new Array that contains the key and value pairs for each index of an array.
Syntax:
array.entries()
Parameters: This method does not accept any parameter.
Return value: It returns an array of indexes and values of the given array on which arr.entries() method is going to work.
More example codes for the above method are as follows:
Program 1:
<script> var arr = [ "HTML" , "CSS" , "JS" , "Bootstrap" , "PHP" ]; var seqn = arr.entries(); document.write( "Applying the Array entries method:" + "<br>" ); for ( var x of seqn) { document.write(x + "</br>" ); } </script> |
Output:
Applying the Array entries method: 0,HTML 1,CSS 2,JS 3,Bootstrap 4,PHP
Program 2:
<script> const x = [ "Geeks" , "for" , "Geeks" ]; for (const [index, element] of x.entries()) document.write(index, element); </script> |
Output:
0Geeks1for2Geeks
Supported Browsers: The browsers supported by JavaScript Array entries() method are listed below:
- Google Chrome 38.0
- Microsoft Edge 12.0
- Mozilla Firefox 28.0
- Safari 8.0
- Opera 25.0