Skip to main content

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();