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 支援。