Computer >> Máy Tính >  >> Lập trình >> Javascript

Hàm TypedArray.entries () trong JavaScript

Hàm entry () của TypedArray trả về một trình lặp của đối tượng TypedArray tương ứng và sử dụng nó, bạn có thể truy xuất các cặp khóa-giá trị của nó. Nơi nó trả về chỉ mục của mảng và phần tử trong chỉ mục cụ thể đó.

Cú pháp

Cú pháp của nó như sau

typedArray.entries()

Ví dụ

<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>

Đầu ra

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

Ví dụ

Nếu bạn cố gắng truy cập phần tử tiếp theo của mảng khi trình vòng lặp trỏ đến cuối mảng, kết quả là bạn sẽ nhận được kết quả là không xác định.

<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>

Đầu ra

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