Lodash _.take() method
What you’ll learn
- How
_.take(array, n)copies the leading prefix without touching the source. - That omitting
nkeeps exactly one element andn = 0yields[]. - When _.slice() or predicate-driven helpers replace blind counts.
- Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
_.tail() returns everything after the first slot; take keeps only the opening segment—compose both when splitting pipelines explicitly.
- You understand shallow copies: nested objects inside the prefix remain aliased.
- You can open Try-it labs or run snippets locally.
Overview
_.take extracts bounded previews—pagination hints, sample rows for dashboards, or argv slices—without manual index juggling.
Prefix-only
Counts strictly from index zero—pair with takeRight variants when trimming the opposite end.
Safe clamping
Over-large counts degenerate into full shallow clones rather than throwing.
Non-destructive
Consumers downstream keep referencing the untouched tail elements.
Syntax
_.take(array, [n=1]) - array: dense collection or array-like value.
- n: non-negative integer count of leading elements to include (defaults to
1). - Returns: new array with at most
nslots copied from the start.
Grab the opening run
Choose how many leading entries survive—ordering stays identical.
import take from "lodash/take";
take(["alpha", "beta", "gamma", "delta"], 2);
// → ["alpha", "beta"] Zero count vs default
n = 0 produces an empty array; omitting n falls back to capturing only the first element.
import take from "lodash/take";
take([10, 20, 30, 40], 0);
// → []
take([10, 20, 30, 40]);
// → [10] Count larger than length
Lodash clamps to the available span—you clone every element without errors.
import take from "lodash/take";
take([1, 2, 3], 99);
// → [1, 2, 3] 📋 _.take vs _.slice, _.drop, _.takeWhile
| API | Primary knob | Use case |
|---|---|---|
_.take(array, n) | Fixed leading count | Known prefix widths such as previews |
_.slice(array, start, end) | Arbitrary half-open window | Offsets not anchored at zero or asymmetric ends |
_.drop(array, n) | Skip leading cells | Inverse framing—keep suffix after skipping |
_.takeWhile(array, predicate) | Logical cutoff | Counts unknown until a predicate fails |
Pitfalls to avoid
Shallow prefix
Nested collections remain referenced—clone deeply only when isolation requires it.
Blind counts
take ignores element values; validate assumptions before slicing sensitive batches.
Sparse arrays
Holes may surface depending on engine behavior—normalize dense arrays first when feasible.
❓ FAQ
Summary
- Purpose:
_.take(array, n)shallow-copies up tonleading elements (defaultn = 1). - Pairs with: _.drop() for complementary skips and _.slice() when bounds differ.
- Next: Lodash _.takeRight(), _.tail() (previous), or the array methods hub.
_.take mirrors _.drop(): prefix versus suffix trims from the same edge—combine both when framing sliding windows without bespoke slice arithmetic.
6 people found this page helpful
