ฟังก์ชัน find() ของ TypedArray ยอมรับค่าสตริงที่แสดงชื่อของฟังก์ชัน ทดสอบว่าองค์ประกอบในอาร์เรย์ผ่านการทดสอบที่ดำเนินการโดยฟังก์ชันที่ให้มาหรือไม่ หากเป็นเช่นนั้น จะส่งกลับองค์ประกอบแรกที่ผ่านการทดสอบอย่างอื่น คืนค่าที่ไม่ได้กำหนด
ไวยากรณ์
ไวยากรณ์ของมันคือดังต่อไปนี้
typedArray.find(function_name)
ตัวอย่าง
<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 ]);
document.write("Contents of the typed array: "+int32View);
document.write("<br>");
function testResult(element, index, array) {
var ele = element>35
return ele;
}
result = int32View.find(testResult);
document.write("Result: "+result);
</script>
</body>
</html> ผลลัพธ์
Contents of the typed array: 64,89,65,21,14,66,87,55 Result: 65
ตัวอย่าง
หาก Array ไม่มีองค์ประกอบที่จำเป็น ฟังก์ชันนี้จะคืนค่า undefined
<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 ]);
document.write("Contents of the typed array: "+int32View);
document.write("<br>");
function testResult(element, index, array) {
var ele = element>100
return ele;
}
result = int32View.find(testResult);
document.write("Result: "+result);
</script>
</body>
</html> ผลลัพธ์
Contents of the typed array: 21,19,65,21,14,66,87,55 Result: undefined