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"]