TypedArray.findIndex() function in JavaScript


The find() function of TypedArray accepts a string value representing the name of a function, tests whether the elements in the array passes the test implemented by the provided function, if so, returns the index of the first element which passes the test else, returns -1.

Syntax

Its Syntax is as follows

typedArray.findIndex(function_name)

Example

 Live Demo

<html>
<head>
   <title>JavaScript Array every Method</title>
</head>
<body>
   <script type="text/javascript">
      var int32View = new Int32Array([21, 19, 65,21, 14, 66, 87, 55 ]);
      document.write("Contents of the typed array: "+int32View);
      document.write("<br>");
      function testResult(element, index, array) {
         var ele = element>35
         return ele;
      }
      result = int32View.findIndex(testResult);
      document.write("Result: "+result);
   </script>
</body>
</html>

Output

Contents of the typed array: 21,19,65,21,14,66,87,55
Result: 2

Example

 Live Demo

<html>
<head>
   <title>JavaScript Array every Method</title>
</head>
<body>
   <script type="text/javascript">
      var int32View = new Int32Array([21, 19, 65,21, 14, 66, 87, 55 ]);
      document.write("Contents of the typed array: "+int32View);
      document.write("<br>");
      function testResult(element, index, array) {
         var ele = element>100
         return ele;
      }
      result = int32View.findIndex(testResult);
      document.write("Result: "+result);
   </script>
</body>
</html>

Output

Contents of the typed array: 21,19,65,21,14,66,87,55
Result: -1

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 25-Jun-2020

12 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements