JavaScript配列copyWithin()
例
最初の2つの配列要素を最後の2つの配列要素にコピーします。
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.copyWithin(2, 0);
最初の2つの配列要素を3番目と4番目の位置にコピーします。
const fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.copyWithin(2, 0, 2);
定義と使用法
このcopyWithin()
メソッドは、配列要素を配列内の別の位置にコピーします。
このcopyWithin()
メソッドは、既存の値を上書きします。
このcopyWithin()
メソッドは、配列に項目を追加しません。
構文
array.copyWithin(target, start, end)
パラメーター
Parameter | Description |
target | Required. The index (position) to copy the elements to. |
start | Optional. The start index (position). Default is 0. |
end | Optional. The end index (position). Default is the array length. |
戻り値
タイプ | 説明 |
配列 | 変更された配列。 |
ブラウザのサポート
copyWithin()
ECMAScript6(ES6)機能です。
ES6(JavaScript 2015)は、最新のすべてのブラウザーでサポートされています。
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
copyWithin()
Internet Explorer 11(またはそれ以前)ではサポートされていません。