TypedArray copyWithin()
JavaScript TypedArray copyWithin()方法
copyWithin()方法复制数组中的数组序列,并在目标位置设置新的起点。 copyWithin()方法是可变方法,可以直接更新数组。它不会改变数组的长度,但是会更改其内容并在必要时创建新的属性。此方法具有三个参数,两个为必需参数,一个为可选参数。
语法:
arr.copyWithin(target)
arr.copyWithin(target, start)
arr.copyWithin(target,start,end)
参数:
target:将元素复制到的索引位置。 (必需)。
start:开始复制索引位置元素。 (可选)
end:它是可选的。
返回值:
修改后的数组。
浏览器支持:
浏览器 |
版本 |
Chrome |
45.0 |
Edge |
12.0 |
Firefox |
32.0 |
Opera |
否 |
示例1
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>
输出:
示例2
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>
输出:
示例3
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>
输出: