Pole JavaScript copyWithin()
Příklady
Zkopírujte první dva prvky pole do posledních dvou prvků pole:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.copyWithin(2, 0);
Zkopírujte první dva prvky pole na třetí a čtvrtou pozici:
const fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.copyWithin(2, 0, 2);
Definice a použití
Metoda copyWithin()
zkopíruje prvky pole na jinou pozici v poli.
Metoda copyWithin()
přepíše existující hodnoty.
Metoda copyWithin()
nepřidává položky do pole.
Syntax
array.copyWithin(target, start, end)
Parametry
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. |
Návratová hodnota
Typ | Popis |
Pole | Změněné pole. |
Podpora prohlížeče
copyWithin()
je funkce ECMAScript6 (ES6).
ES6 (JavaScript 2015) je podporován ve všech moderních prohlížečích:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
copyWithin()
není podporována v aplikaci Internet Explorer 11 (nebo starší).