JavaScript带示例的typedArray.includes()

2021年5月5日13:44:09 发表评论 94 次浏览

typedArray.includes()是JavaScript中的内置函数, 用于检查给定的typedArray是否包含特定元素, 并因此返回true和false。

语法如下:

typedarray.includes(Element, Index);

参数:它接受下面指定的两个参数-

元素:是在typedArray中搜索的元素。

索引:它是应该开始搜索的typedArray形式中元素的索引。其默认值为零(0), 并且是可选的。

返回值:

如果元素存在于给定的typedArray中, 则它返回布尔值true, 否则返回false。

JavaScript代码显示此功能的工作方式:

代码1:

<script>
  
     //Creating some typedArrays
     const A = new Uint8Array([ 1, 2, 3, 4, 5 ]);
     const B = new Uint8Array([ 5, 10, 15, 20 ]);
     const C = new Uint8Array([ 0, 2, 4, 6, , 8, 10 ]);
     const D = new Uint8Array([ 1, 3, 5, 7, 9 ]);
  
     //Calling include() function
     a = A.includes(2)
     b = B.includes(15, 1)
     c = C.includes(6)
     d = D.includes(9, 1)
  
     //Printing true or false, either the element
     //is present in the typedArray or not
     document.write(a + "<br>" );
     document.write(b + "<br>" );
     document.write(c + "<br>" );
     document.write(d);
  
</script>

输出如下:

true
true
true
true

代码2:

<script>
  
     //Creating some typedArrays
     const A = new Uint8Array([ 1, 2, 3, 4, 5 ]);
     const B = new Uint8Array([ 5, 10, 15, 20 ]);
     const C = new Uint8Array([ 0, 2, 4, 6, , 8, 10 ]);
     const D = new Uint8Array([ 1, 3, 5, 7, 9 ]);
  
     //Calling include() function
     a = A.includes(6)
     b = B.includes(21, 1)
     c = C.includes(6, 4)
     d = D.includes(0)
  
     //Printing true or false, either the element
     //is present in the typedArray or not
     document.write(a + "<br>" );
     document.write(b + "<br>" );
     document.write(c + "<br>" );
     document.write(d);
  
</script>

输出如下:

false
false
false
false

一盏木

发表评论

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