JavaScript Array copyWithin() 方法
示例
复制到索引 2,从索引 0 开始的所有元素
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.copyWithin(2, 0);
自己动手试一试 »
复制到索引 2,从索引 0 到 2 的元素
const fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.copyWithin(2, 0, 2);
自己动手试一试 »
描述
copyWithin()
方法将数组元素复制到数组中的另一个位置。
copyWithin()
方法会覆盖现有值。
copyWithin()
方法不会向数组添加元素。
语法
array.copyWithin(target, start, end)
参数
参数 | 描述 |
target | 必需。 要将元素复制到的索引(位置)。 |
start | 可选。 开始索引(位置)。默认为 0。 |
end | 可选。 结束索引(位置)。默认为数组长度。 |
返回值
类型 | 描述 |
数组 | 已更改的数组。 |
浏览器支持
copyWithin()
是 ECMAScript6 (ES6) 功能。
自 2017 年 6 月起,所有现代浏览器都支持 ES6 (JavaScript 2015)
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
2016 年 5 月 | 2017 年 4 月 | 2017 年 6 月 | 2016 年 9 月 | 2016 年 6 月 |
copyWithin()
不受 Internet Explorer 支持。