รายการ () ฟังก์ชันของ TypedArray ส่งคืนตัววนซ้ำของวัตถุ TypedArray ที่เกี่ยวข้อง และใช้สิ่งนี้ คุณสามารถดึงข้อมูลคู่ของคีย์-ค่าของมันได้ โดยจะส่งคืนดัชนีของอาร์เรย์และองค์ประกอบในดัชนีนั้น ๆ
ไวยากรณ์
ไวยากรณ์ของมันคือดังต่อไปนี้
typedArray.entries()
ตัวอย่าง
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var int32View = new Int32Array([21, 64, 89, 65, 33, 66, 87, 55]);
document.write("Contents of the typed array: "+int32View);
document.write("<br>");
var it = int32View.entries();
for(i=0; i<int32View.length; i++) {
document.write(it.next().value);
document.write("<br>");
}
</script>
</body>
</html> ผลลัพธ์
Contents of the typed array: 21,64,89,65,33,66,87,55 0,21 1,64 2,89 3,65 4,33 5,66 6,87 7,55
ตัวอย่าง
หากคุณพยายามเข้าถึงองค์ประกอบถัดไปของอาร์เรย์เมื่อตัววนซ้ำชี้ไปที่ส่วนท้ายของอาร์เรย์ ผลลัพธ์จะไม่ถูกกำหนดเป็นผลลัพธ์
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var int32View = new Int32Array([21, 64, 89, 65, 33, 66, 87, 55]);
document.write("Contents of the typed array: "+int32View);
document.write("<br>");
var it = int32View.entries();
for(i=0; i<int32View.length; i++) {
document.write(it.next().value);
document.write("<br>");
}
document.write(it.next().value);
</script>
</body>
</html> ผลลัพธ์
Contents of the typed array: 21,64,89,65,33,66,87,55 0,21 1,64 2,89 3,65 4,33 5,66 6,87 7,55 undefined