Skip to main content

initial()

initial<T>(array): T[]

Gets all but the last element of array.

DEPRECATED

Use array.slice(0, -1) 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 query.


Returns: T[]โ€‹

A new array with all elements except the last.


See Alsoโ€‹


Sinceโ€‹

2.0.0


Also known asโ€‹

dropLast (Remeda) ยท init (Ramda, Effect) ยท initial (Lodash, es-toolkit) ยท โŒ (Radashi, Modern Dash, Antfu)


Exampleโ€‹

const numbers = [1, 2, 3, 4, 5];

// โŒ Deprecated approach
const withoutLast = initial(numbers);
console.log(withoutLast); // [1, 2, 3, 4]

// โœ… Recommended approach
const withoutLastNative = numbers.slice(0, -1);
console.log(withoutLastNative); // [1, 2, 3, 4]

How it works?โ€‹

Gets all but the last element of array. Deprecated: Use array.slice(0, -1) directly.

Native Equivalentโ€‹

// โŒ initial(arr)
// โœ… arr.slice(0, -1)

Use Casesโ€‹

Get all but last element ๐Ÿ“Œโ€‹

Get array without the last element.

const items = [1, 2, 3, 4, 5];
items.slice(0, -1);
// => [1, 2, 3, 4]

Process all but currentโ€‹

Get previous items excluding the current one.

const history = ["page1", "page2", "page3"];
history.slice(0, -1);
// => ["page1", "page2"]

Remove trailing elementโ€‹

Create new array without the last item.

const path = ["home", "docs", "file.txt"];
path.slice(0, -1);
// => ["home", "docs"]