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

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


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

ไวยากรณ์

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

typedArray.filter(callbackFunction)

ตัวอย่าง

<html> <head>    <title>ตัวอย่าง TypedArray filter</title> </head> <body>    <script type="text/javascript">       var int32View = new Int32Array([64, 89, 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.filter(testResult);       document.write("องค์ประกอบที่มากกว่า 35: " + result);    </script> </body> </html>

ผลลัพธ์

เนื้อหาของ Typed Array: 64,89,65,21,14,66,87,55 องค์ประกอบที่มากกว่า 35: 64,89,65,66,87,55