slice()
slice<
T>(array,start?,end?):T[]
Creates a slice of array from start up to, but not including, end.
DEPRECATED
Use array.slice(start, end) directly instead.
Reason:
Native equivalent method now available
Type Parametersβ
T: Tβ
The type of elements in the array.
Parametersβ
array: T[]β
The array to slice.
start?: numberβ
The start position.
end?: numberβ
The end position.
Returns: T[]β
A new sliced array.
See Alsoβ
Sinceβ
2.0.0
Also known asβ
slice (Lodash, es-toolkit, Remeda, Ramda) Β· β (Radashi, Effect, Modern Dash, Antfu)
Exampleβ
const numbers = [1, 2, 3, 4, 5];
// β Deprecated approach
const sliced = slice(numbers, 1, 4);
console.log(sliced); // [2, 3, 4]
// β
Recommended approach
const slicedNative = numbers.slice(1, 4);
console.log(slicedNative); // [2, 3, 4]
How it works?β
Creates a shallow copy of a portion of an array.
Deprecated: Use array.slice() directly.
Native Equivalentβ
// β slice(arr, 1, 4)
// β
arr.slice(1, 4)
Use Casesβ
Get portion of array πβ
Extract a subset of elements.
const items = [1, 2, 3, 4, 5];
items.slice(1, 4);
// => [2, 3, 4]
Paginate resultsβ
Get a page of results from an array.
const all = [...allItems];
const page = all.slice(offset, offset + limit);
Clone array immutablyβ
Create a shallow copy of an array.
const original = [1, 2, 3];
const copy = original.slice();