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