该entries()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