Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ฟังก์ชัน TypedArray.find() ใน JavaScript


ฟังก์ชัน find() ของ TypedArray จะรับฟังก์ชัน callback ทดสอบแต่ละองค์ประกอบในอาร์เรย์ หากองค์ประกอบใดผ่านการทดสอบ จะคืนค่าองค์ประกอบแรกที่ผ่านการทดสอบ มิฉะนั้นจะคืนค่า undefined

ไวยากรณ์

ไวยากรณ์มีดังนี้

typedArray.find(callbackFunction)

ตัวอย่าง

<html> <head>    <title>ตัวอย่าง TypedArray find</title> </head> <body>    <script type="text/javascript">       var int32View = new Int32Array([21, 19, 65, 21, 14, 66, 87, 55 ]);       document.write("เนื้อหาของ Typed Array: " + int32View);       document.write("<br>");       function testResult(element, index, array) {          var ele = element > 35;          return ele;       }       result = int32View.find(testResult);       document.write("ผลลัพธ์: " + result);    </script> </body> </html>

ผลลัพธ์

เนื้อหาของ Typed Array: 21,19,65,21,14,66,87,55 ผลลัพธ์: 65

ตัวอย่าง: ไม่พบองค์ประกอบที่ผ่านการทดสอบ

<html> <head>    <title>ตัวอย่าง TypedArray find</title> </head> <body>    <script type="text/javascript">       var int32View = new Int32Array([21, 19, 65, 21, 14, 66, 87, 55 ]);       document.write("เนื้อหาของ Typed Array: " + int32View);       document.write("<br>");       function testResult(element, index, array) {          var ele = element > 100;          return ele;       }       result = int32View.find(testResult);       document.write("ผลลัพธ์: " + result);    </script> </body> </html>

ผลลัพธ์

เนื้อหาของ Typed Array: 21,19,65,21,14,66,87,55 ผลลัพธ์: undefined