ฟังก์ชัน indexOf() ของวัตถุ TypedArray ยอมรับค่าและตรวจสอบว่าอาร์เรย์ที่พิมพ์มีองค์ประกอบที่ระบุหรือไม่ ถ้าเป็นเช่นนั้น ฟังก์ชันนี้จะคืนค่าดัชนีของอาร์เรย์ที่พบองค์ประกอบที่ระบุ หากองค์ประกอบนั้นเกิดขึ้นหลายครั้ง ฟังก์ชันนี้จะคืนค่าดัชนีแรกระหว่างองค์ประกอบเหล่านั้น หากอาร์เรย์ไม่มีองค์ประกอบที่ระบุ ฟังก์ชัน indexOf() จะคืนค่า -1
ไวยากรณ์
ไวยากรณ์ของมันคือดังต่อไปนี้
typedArray.indexOf(50)
ตัวอย่าง
<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, 66, 97, 66 ]);
document.write("Contents of the typed array: "+int32View);
document.write("<br>");
var contains = int32View.indexOf(66);
document.write("Specified element occurred at the index: "+contains);
</script>
</body>
</html> ผลลัพธ์
: Contents of the typed array: 21,19,65,21,14,66,87,55 Specified element occurred at the index: 5
ตัวอย่าง
<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, 66, 97, 66 ]);
document.write("Contents of the typed array: "+int32View);
document.write("<br>");
var contains = int32View.indexOf(634);
document.write("Specified element occurred at the index: "+contains);
</script>
</body>
</html> ผลลัพธ์
: Contents of the typed array: 21,19,65,21,14,66,87,55 Specified element occurred at the index: -1