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

Hàm TypedArray.includes() trong JavaScript

Hàm includes() của TypedArray kiểm tra xem mảng có chứa một giá trị xác định hay không. Trả về true nếu có, ngược lại false. Sử dụng so sánh SameValueZero (NaN === NaN là true).

Cú pháp

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

typedArray.includes(searchElement[, fromIndex])

Ví dụ

<html> <head>    <title>Ví dụ JavaScript TypedArray includes</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>");       var contains = int32View.includes(66);       if(contains) {          document.write("Mảng chứa phần tử chỉ định");       } else {          document.write("Mảng không chứa phần tử chỉ định");       }    </script> </body> </html>

Kết quả

Nội dung mảng typed: 21,19,65,21,14,66,87,55
Mảng chứa phần tử chỉ định

Ví dụ 2

<html> <head>    <title>Ví dụ JavaScript TypedArray includes</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>");       var contains = int32View.includes(762);       if(contains) {          document.write("Mảng chứa phần tử chỉ định");       } else {          document.write("Mảng không chứa phần tử chỉ định");       }    </script> </body> </html>

Kết quả

Nội dung mảng typed: 21,19,65,21,14,66,87,55
Mảng không chứa phần tử chỉ định