Lodash _.initial() method
What you’ll learn
- How
_.initial(array)returns every element except the last in a new array. - Why
[]and[42]both yield[], and how element references are copied shallowly. - When to prefer
slice(0, -1)or _.dropRight() instead. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Optional prior read _.indexOf(); you should know that arrays are zero-based and that many Lodash array helpers return new arrays.
- You are comfortable with
sliceend indexes and the idea of a “non-mutating” copy. - You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.initial is the “drop the last element” helper: you keep the prefix of the array and omit the final slot. It mirrors how you might describe “everything except the footer row” when processing tabular data.
Prefix copy
Length decreases by one when the input has at least one element; otherwise you still get an empty array.
Shallow
Nested objects are not cloned; the new array holds the same references as the original prefix.
Complements last
Combine with _.last when you want to split “body” and “tail” without manual index math.
Syntax
_.initial(array) - array: collection to process (array-like values are supported).
- Returns: a new array of all elements except the last, or
[]when there is no element to keep.
Typical list
The last value is removed; order of the remaining items is unchanged.
import initial from "lodash/initial";
initial([1, 2, 3, 4]);
// [1, 2, 3] Empty and single-element arrays
With zero or one element there is no “prefix before the last,” so the result is always [].
import initial from "lodash/initial";
JSON.stringify({
emptyInput: initial([]),
oneElement: initial(["only"])
});
// '{"emptyInput":[],"oneElement":[]}' Objects stay shared
initial builds a new array wrapper; the objects inside are the same references as in the source.
import initial from "lodash/initial";
const a = { n: 1 };
const b = { n: 2 };
const c = { n: 3 };
const rest = initial([a, b, c]);
rest[0] === a && rest[1] === b;
// true 📋 _.initial vs native slice vs _.dropRight
| API | What it does | Note |
|---|---|---|
_.initial(array) | All elements except the last | Lodash import; array-like inputs |
array.slice(0, -1) | Same indices for real arrays | Built-in; no Lodash dependency |
_.dropRight(array, 1) | Drop one element from the end | Generalizes to n > 1; same idea for trailing removal |
Pitfalls to avoid
One element feels surprising
initial([x]) is [], not [x]. If you need “all but last unless length is 1,” branch on length first.
Not a deep clone
Mutating an object still visible through initial’s result changes the “same” object in the original array.
Do not confuse with tail
_.tail removes the first element. Use the name that matches whether you drop the head or the foot of the list.
❓ FAQ
Summary
- Purpose:
_.initial(array)returns a new array containing every element except the last. - Edges:
[]and one-element arrays both produce[]. - Next: Lodash _.intersection(), _.indexOf() (earlier in this track), or the array methods hub.
Lodash pairs _.initial with _.last: initial drops the tail element; _.last keeps only that element.
6 people found this page helpful
