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

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

TypedArray.subarray()

เมธอด subarray() ส่งคืน Typed Array ใหม่ที่เป็นส่วนย่อย (Slice) ของ Array เดิม โดยรับพารามิเตอร์ 2 ค่า คือ begin (Index เริ่มต้น) และ end (Index สิ้นสุด ไม่รวม)

ไวยากรณ์

typedArray.subarray(begin, end)

ตัวอย่าง

<html> <body> <script>
  var typedArray = new Int32Array([111, 56, 62, 40, 75, 36, 617, 2, 139, 827]);
  var result = typedArray.subarray(3, 7); // Index 3 ถึง 6
  document.write("Contents of the typed array: " + result);
</script> </body> </html>

ผลลัพธ์

Contents of the typed array: 40,75,36,617

คำอธิบาย: subarray() ไม่สร้างข้อมูลใหม่ (Zero-copy) แต่สร้าง View ใหม่ที่อ้างอิง Buffer เดียวกัน การเปลี่ยนแปลงค่าใน subarray จะส่งผลต่อ Array ต้นฉบับด้วย