JavaScript Array slice()
Příklady
Vyberte prvky:
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(1, 3);
Vyberte prvky pomocí záporných hodnot:
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const myBest = fruits.slice(-3, -1);
Definice a použití
Metoda slice()
vrací vybrané prvky v poli jako nové pole.
Metoda slice()
vybírá od daného začátku až po (nezahrnující) daný konec .
Metoda slice()
nemění původní pole.
Syntax
array.slice(start, end)
Parametry
Parameter | Description |
start | Optional. Start position. Default is 0. Negative numbers select from the end of the array. |
end | Optional. End position. Default is last element. Negative numbers select from the end of the array. |
Návratová hodnota
Nové pole obsahující vybrané prvky. |
Podpora prohlížeče
slice()
je funkce ECMAScript1 (ES1).
ES1 (JavaScript 1997) je plně podporován ve všech prohlížečích:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |