JavaScript |类型Darray。条目()和示例

这个 达雷。条目() 是JavaScript中的一个内置函数,它提供了一个新的数组迭代器对象,其中包含给定typedArray对象的键和值对。

null

语法:

typedArray.entries()

参数: 它不接受任何参数。

返回值 它返回一个新的数组迭代器对象,其中包含给定typedArray对象的键和值对。

显示此函数工作的JavaScript代码:
代码#1:

<script>
// Creating a typedArray Uint8Array() with some elements
const uint8 = new Uint8Array([ 5, 10, 15, 20, 25, 30 ]);
// Calling entries() function
A = uint8.entries();
// Shifting array iterator to next element one by one
// Iterator assigned to 10
A.next();
// Iterator assigned to 15
A.next();
document.write(A.next().value);
</script>


输出:

2, 15

这里2是元素15的索引。 代码#2:

<script>
// Creating a typedArray Uint8Array() with some elements
const uint8 = new Uint8Array([ 5, 10, 15, 20, 25 ]);
// Calling entries() function
A = uint8.entries();
// Shifting array iterator to next element one by one
// Iterator assigned to 10
A.next();
// Iterator assigned to 15
A.next();
// Iterator assigned to 20
A.next();
// Iterator assigned to 25
A.next();
// Iterator went out of index
A.next();
document.write(A.next().value);
</script>


输出:

undefined

这里输出是未定义的,因为迭代器超过了上限。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享