Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Hàm TypedArray.findIndex() trong JavaScript

Hàm findIndex() của TypedArray trả về chỉ số của phần tử đầu tiên thỏa mãn điều kiện do hàm callback cung cấp. Trả về -1 nếu không tìm thấy phần tử nào thỏa mãn.

Cú pháp

Cú pháp của nó như sau:

typedArray.findIndex(callbackFn[, thisArg])

Ví dụ

<html> <head>    <title>Ví dụ JavaScript TypedArray findIndex</title> </head> <body>    <script type="text/javascript">       var int32View = new Int32Array([21, 19, 65, 21, 14, 66, 87, 55]);       document.write("Nội dung mảng typed: "+int32View);       document.write("<br>");       function testResult(element, index, array) {          return element > 35;       }       var result = int32View.findIndex(testResult);       document.write("Kết quả: "+result);    </script> </body> </html>

Kết quả

Nội dung mảng typed: 21,19,65,21,14,66,87,55
Kết quả: 2

Ví dụ 2 - Không tìm thấy

<html> <head>    <title>Ví dụ JavaScript TypedArray findIndex</title> </head> <body>    <script type="text/javascript">       var int32View = new Int32Array([21, 19, 65, 21, 14, 66, 87, 55]);       document.write("Nội dung mảng typed: "+int32View);       document.write("<br>");       function testResult(element, index, array) {          return element > 100;       }       var result = int32View.findIndex(testResult);       document.write("Kết quả: "+result);    </script> </body> </html>

Kết quả

Nội dung mảng typed: 21,19,65,21,14,66,87,55
Kết quả: -1