Syntax error TypedArray.entries() function in JavaScript

TypedArray.entries() function in JavaScript



The entries() function of TypedArray returns an iterator of the corresponding TypedArray object and using this, you can retrieve the key-value pairs of it. Where it returns the index of the array and the element in that particular index.

Syntax

Its Syntax is as follows

typedArray.entries()

Example

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var int32View = new Int32Array([21, 64, 89, 65, 33, 66, 87, 55]);
      document.write("Contents of the typed array: "+int32View);
      document.write("<br>");
      var it = int32View.entries();
      for(i=0; i<int32View.length; i++) {
         document.write(it.next().value);
         document.write("<br>");
      }
   </script>
</body>
</html>

Output

Contents of the typed array: 21,64,89,65,33,66,87,55
0,21
1,64
2,89
3,65
4,33
5,66
6,87
7,55

Example

If you try to access the next element of the array when the iterator points to the end of the array you will get undefined as result.

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var int32View = new Int32Array([21, 64, 89, 65, 33, 66, 87, 55]);
      document.write("Contents of the typed array: "+int32View);
      document.write("<br>");
      var it = int32View.entries();
      for(i=0; i<int32View.length; i++) {
         document.write(it.next().value);
         document.write("<br>");
      }
      document.write(it.next().value);
   </script>
</body>
</html>

Output

Contents of the typed array: 21,64,89,65,33,66,87,55
0,21
1,64
2,89
3,65
4,33
5,66
6,87
7,55
undefined
Updated on: 2020-06-25T13:14:17+05:30

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements