JavaScript |类型Darray。subarray()及其示例

这个 达雷。子数组() 是JavaScript中的一个内置函数,用于返回typedArray对象的一部分。 语法:

null
typedarray.subarray(begin, end)

参数: 它接受以下两个参数:

  1. 开始: 它指定起始元素的索引,给定数组的部分将从该元素开始。它是可选的和包容性的。
  2. 完: 它指定要包含给定数组部分的结束元素的索引。它是可选的和专用的。

返回值: 它返回一个由给定的typedArray对象组成的新数组。

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

<script>
// Creating a new typedArray Uint8Array() object
const A = new Uint8Array([5, 10, 15, 20, 25, 30, 35 ]);
// Calling subarray() functions
B = A.subarray(1, 3)
C = A.subarray(1)
D = A.subarray(3)
E = A.subarray(0, 6)
F = A.subarray(0)
// Printing some new typedArray which are
// the part of the given input typedArray
document.write(B + "<br>" );
document.write(C + "<br>" );
document.write(D + "<br>" );
document.write(E + "<br>" );
document.write(F + "<br>" );
</script>


输出:

10,15
10,15,20,25,30,35
20,25,30,35
5,10,15,20,25,30
5,10,15,20,25,30,35

代码#2: 当索引为负数时,可以从typedArray对象的末尾访问元素。

下面是说明这种负面索引概念的必要代码。

<script>
// Creating a new typedArray Uint8Array() object
const A = new Uint8Array([5, 10, 15, 20, 25, 30, 35 ]);
// Calling subarray() functions
B = A.subarray(-1)
C = A.subarray(-2)
D = A.subarray(-3)
E = A.subarray(3)
F = A.subarray(0)
// Printing some new typedArray which are
// the part of the given input typedArray
document.write(B + "<br>" );
document.write(C + "<br>" );
document.write(D + "<br>" );
document.write(E + "<br>" );
document.write(F + "<br>" );
</script>


输出:

35
30,35
25,30,35
20,25,30,35
5,10,15,20,25,30,35
© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享