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

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

TypedArray.values()

เมธอด values() ส่งคืน Iterator Object ที่สามารถวนลูปเข้าถึงค่าแต่ละตัวใน Typed Array ได้ ใช้ร่วมกับ next() หรือลูป for...of

ไวยากรณ์

typedArray.values()

ตัวอย่างการใช้งาน

<html> <body> <script>
  var typedArray = new Int32Array([11, 5, 13, 4, 15, 3, 17, 2, 19, 8]);
  var iterator = typedArray.values();
  document.write("Contents of the typed array: ");
  for (let i = 0; i < typedArray.length; i++) {
    document.writeln(iterator.next().value);
  }
</script> </body> </html>

ผลลัพธ์

Contents of the typed array: 11 5 13 4 15 3 17 2 19 8

แนวทางปัจจุบัน (Modern): ควรใช้ for...of แทนเพื่อความอ่านง่ายกว่า:

for (const value of typedArray.values()) {
  console.log(value);
}