这个 达雷。子数组() 是JavaScript中的一个内置函数,用于返回typedArray对象的一部分。 语法:
null
typedarray.subarray(begin, end)
参数: 它接受以下两个参数:
- 开始: 它指定起始元素的索引,给定数组的部分将从该元素开始。它是可选的和包容性的。
- 完: 它指定要包含给定数组部分的结束元素的索引。它是可选的和专用的。
返回值: 它返回一个由给定的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, 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