JavaScript配列slice()
例
要素を選択します。
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const citrus = fruits.slice(1, 3);
負の値を使用して要素を選択します。
const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
const myBest = fruits.slice(-3, -1);
定義と使用法
このslice()
メソッドは、配列内の選択された要素を新しい配列として返します。
このslice()
メソッドは、指定された開始から(包括的ではない)指定された終了までを選択します。
このslice()
メソッドは元の配列を変更しません。
構文
array.slice(start, end)
パラメーター
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. |
戻り値
選択した要素を含む新しい配列。 |
ブラウザのサポート
slice()
ECMAScript1(ES1)機能です。
ES1(JavaScript 1997)は、すべてのブラウザーで完全にサポートされています。
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |