JavaScript 带示例的typedArray.entries()

2021年5月6日17:03:38 发表评论 94 次浏览

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

语法如下:

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

由于迭代器超出上限, 因此输出未定义。


一盏木

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: