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

Hàm TypedArray.find() trong JavaScript

Hàm find() của TypedArray trả về giá trị 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ề undefined 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.find(callbackFn[, thisArg])

Ví dụ

<html> <head>    <title>Ví dụ JavaScript TypedArray find</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.find(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ả: 65

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

<html> <head>    <title>Ví dụ JavaScript TypedArray find</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.find(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ả: undefined