• JavaScript Video Tutorials

JavaScript - TypedArray keys() Method



The JavaScript TypedArray keys() method returns an array iterator object that contains the keys for each index of the typed array. A key in JavaScript is a unique identifier used to access values in an object.

Syntax

Following is the syntax of JavaScript TypedArray keys() method −

keys()

Parameters

  • It does not accept any parameters.

Return value

This method returns a new array iterator object.

Examples

Example 1

In the following program, we are using the JavaScript TypedArray keys() method to retrieve an array iterator object of this typed array [10, 20, 30, 40, 50].

<html>
<head>
   <title>JavaScript TypedArray keys() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([10, 20, 30, 40, 50]);
      document.write("The typed arrays is: ", T_array);
      document.write("<br>The T_array.keys() method returns: ", T_array.keys());
   </script>    
</body>
</html>

Output

The above program return '[object Array Iterator]' in the output.

The typed arrays is: 10,20,30,40,50
The T_array.keys() method returns: [object Array Iterator]

Example 2

The following is another example of the JavaScript TypedArray keys(). We use this method to retrieve an array iterator object containing keys of each index of this typed array [1, 2, 3, 4, 5, 6, 7, 8].

<html>
<head>
   <title>JavaScript TypedArray keys() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([10, 20, 30, 40, 50]);
      document.write("The typed arrays is: ", T_array);
      document.write("<br>The T_array.keys() method returns: ", T_array.keys());
      const arrayKeys = T_array.keys();
      document.write("<br>The typed array keys and respected values are: <br>")
      for(const n of arrayKeys){
         document.write("Key = ", n, " , Value = ", T_array[n], "<br>");
      }
   </script>    
</body>
</html>

Output

After executing the above program, it will display keys on each index along with values as −

The typed arrays is: 10,20,30,40,50
The T_array.keys() method returns: [object Array Iterator]
The typed array keys and respected values are:
Key = 0 , Value = 10
Key = 1 , Value = 20
Key = 2 , Value = 30
Key = 3 , Value = 40
Key = 4 , Value = 50

Example 3

In this program, we use the keys() method to retrieve an array iterator object containing the keys of each index in the typed array [1, 2, 3, 4, 5]. Then, we store all keys in a variable and perform an alternative iteration.

<html>
<head>
   <title>JavaScript TypedArray keys() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([1, 2, 3, 4, 5]);
      document.write("The typed arrays is: ", T_array);
      const allKeys = T_array.keys();
      document.write("<br>");
      document.write("Key1 = ", allKeys.next().value, "<br>");
      document.write("Key2 = ", allKeys.next().value, "<br>");
      document.write("Key3 = ", allKeys.next().value, "<br>");
      document.write("Key4 = ", allKeys.next().value, "<br>");
      document.write("Key5 = ", allKeys.next().value);
   </script>    
</body>
</html>

Output

Once the above program is executed, it will return the keys of each index as −

The typed arrays is: 1,2,3,4,5
Key1 = 0
Key2 = 1
Key3 = 2
Key4 = 3
Key5 = 4
Advertisements