What is the use of Array.entries() method in JavaScript?

JavascriptObject Oriented ProgrammingProgramming

Array.entries()

The Array.entries() in JavaScript is used to get a new Array that contains the key and value pairs for each index of an array. It returns an Array Iterator object with key/value pairs.

syntax

array.entries();

Example

In the following example elements.entries() method, using for loop, access every element and generate key/value pair of the particular element.To simplify the code we just assigned an iterator variable to elements.entries().

.Live Demo

<html>
<body>
<script>
   var elements = ["Helium", "Neon", "Krypton", "Xenon", "Radon"];
   var iterator = elements.entries();
   for (let e of iterator) {
      document.write(e);
      document.write("</br>");
   }
</script>
</body>
</html>

Output

0,Helium
1,Neon
2,Krypton
3,Xenon
4,Radon
raja
Published on 29-May-2019 13:38:09
Advertisements