Hàm indexOf() của TypedArray tìm kiếm một giá trị từ đầu mảng, trả về chỉ số của lần xuất hiện đầu tiên. Trả về -1 nếu không tìm thấy. Tìm kiếm dùng so sánh nghiêm ngặt (===).
Cú pháp
Cú pháp của nó như sau:
typedArray.indexOf(searchElement[, fromIndex])
Ví dụ
<html> <head> <title>Ví dụ JavaScript TypedArray indexOf</title> </head> <body> <script type="text/javascript"> var int32View = new Int32Array([21, 19, 65, 21, 14, 66, 87, 55, 66, 97, 66]); document.write("Nội dung mảng typed: "+int32View); document.write("<br>"); var contains = int32View.indexOf(66); document.write("Phần tử xuất hiện đầu tiên tại chỉ số: "+contains); </script> </body> </html>
Kết quả
Nội dung mảng typed: 21,19,65,21,14,66,87,55,66,97,66 Phần tử xuất hiện đầu tiên tại chỉ số: 5
Ví dụ 2 - Không tìm thấy
<html> <head> <title>Ví dụ JavaScript TypedArray indexOf</title> </head> <body> <script type="text/javascript"> var int32View = new Int32Array([21, 19, 65, 21, 14, 66, 87, 55, 66, 97, 66]); document.write("Nội dung mảng typed: "+int32View); document.write("<br>"); var contains = int32View.indexOf(634); document.write("Phần tử xuất hiện tại chỉ số: "+contains); </script> </body> </html>
Kết quả
Nội dung mảng typed: 21,19,65,21,14,66,87,55,66,97,66 Phần tử xuất hiện tại chỉ số: -1