Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Hàm TypedArray.entries() trong JavaScript

Hàm entries() của TypedArray trả về một đối tượng Iterator mới chứa cặp [index, value] cho mỗi phần tử trong TypedArray, theo thứ tự chỉ số.

Cú pháp

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

typedArray.entries()

Ví dụ

<html> <head>    <title>Ví dụ JavaScript</title> </head> <body>    <script type="text/javascript">       var int32View = new Int32Array([21, 64, 89, 65, 33, 66, 87, 55]);       document.write("Nội dung mảng typed: "+int32View);       document.write("<br>");       var it = int32View.entries();       for(let i = 0; i < int32View.length; i++) {          document.write(it.next().value);          document.write("<br>");       }    </script> </body> </html>

Kết quả

Nội dung mảng typed: 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ụ 2

Nếu truy cập phần tử tiếp theo khi iterator đã hết mảng, kết quả sẽ là undefined.

<html> <head>    <title>Ví dụ JavaScript</title> </head> <body>    <script type="text/javascript">       var int32View = new Int32Array([21, 64, 89, 65, 33, 66, 87, 55]);       document.write("Nội dung mảng typed: "+int32View);       document.write("<br>");       var it = int32View.entries();       for(let i = 0; i < int32View.length; i++) {          document.write(it.next().value);          document.write("<br>");       }       document.write(it.next().value);    </script> </body> </html>

Kết quả

Nội dung mảng typed: 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