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

这个 达雷。copyWithin() 是JavaScript中的一个内置函数,用于在同一typedArray中的指定位置复制typedArray的某些元素。

null

语法:

typedArray.copyWithin(target, start, end)

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

  • 目标: 它是复制元素的起始索引位置。
  • 开始: 它是开始复制元素的起始索引位置,其默认值是Darray类型的起始索引。
  • 完: 它是可选的,是要复制的元素的结束位置索引,其默认值是typedArray的结束。

    返回值: 复制过程完成后,它返回修改后的数组。

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

    <script>
    // Constructing a new typedArray "A"
    // with some elements
    const A = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
    // Calling copyWithin function to copy
    // element from index position 0 and
    // element from index 4 to 5
    A.copyWithin(0, 4, 5);
    // Printing a new modified array
    document.write(A);
    </script>

    
    

    输出:

    25,10,15,20,25,30,35,40

    代码#2:

    <script>
    // Constructing some new typedArrays
    // with some elements
    const A = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
    const B = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
    const C = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
    const D = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
    const E = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
    // Calling copyWithin function with different
    // parameters
    a = A.copyWithin(0, 5);
    b = B.copyWithin(1, 4);
    c = C.copyWithin(0, 4, 5);
    d = D.copyWithin(2, 3, 5);
    e = E.copyWithin(0, 1, 4);
    // Printing new modified arrays
    document.write(a + "<br>" );
    document.write(b + "<br>" );
    document.write(c + "<br>" );
    document.write(d + "<br>" );
    document.write(e);
    </script>

    
    

    输出:

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