copyWithin()方法复制数组中的数组序列,并在目标位置设置新的起点。 copyWithin()方法是可变方法,可以直接更新数组。它不会改变数组的长度,但是会更改其内容并在必要时创建新的属性。此方法具有三个参数,两个为必需参数,一个为可选参数。
arr.copyWithin(target) arr.copyWithin(target, start) arr.copyWithin(target,start,end)
target - 将元素复制到的索引位置。 (需要)。
target - 开始复制索引位置元素。 (可选的)
end - 它是可选的。从中结束复制元素的源结束索引位置。
修改后的数组。
Chrome | 45.0 |
Edge | 12.0 |
Firefox | 32.0 |
Opera | no |
JavaScript TypedArray copyWithin(target)方法
<script type="text/javascript"> //Input array // JavaScript to illustrate copyWithin() method var arr1= [1,2,3,4,5,6,7,8,9,10]; arr1.copyWithin(2) //Placing from index position 2 //The element from index 0 document.write(arr1); // expected output: arr1 [Output:1,2,1,2,3,4,5,6,7,8] </script>
输出:
1,2,1,2,3,4,5,6,7,8
JavaScript TypedArray copyWithin(target,start)方法
<script type="text/javascript"> // Input array // JavaScript to illustrate copyWithin() method var arr1= [1,2,3,4,5,6,7,8,9,10]; arr1.copyWithin(2,3) //Placing from index position 2 // Element from index 3 document.write(arr1); // expected output: arr1 [Output: 1,2,4,5,6,7,8,9,10,10] </script>
输出:
1,2,4,5,6,7,8,9,10,10
JavaScript TypedArray copyWithin(target,start,end)方法
<script type="text/javascript"> //Input array // JavaScript to illustrate copyWithin() method var arr1= [1,2,3,4,5,6,7,8,9,10]; arr1.copyWithin(1,2,4) // Placing at index position 1 // Element between index 2 and 4 document.write(arr1); // expected output: arr1 [Output: 1,3,4,4,5,6,7,8,9,10] </script>
输出:
1,3,4,4,5,6,7,8,9,10
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)