Lodash _.flattenDepth() method
What you’ll learn
- How
_.flattenDepth(array, depth)unwraps exactlydepthlevels of array nesting. - Why that sits between _.flatten() (one level) and _.flattenDeep() (all levels).
- How it lines up with
Array.prototype.flat(depth)on real arrays. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.flatten() and _.flattenDeep() on this site so the three APIs form one mental model.
- You can read
depthas “how many times to peel array wrappers from the current structure.” - You can run snippets in Node or open the Try-it labs in a browser.
Overview
_.flattenDepth is the controlled middle ground: you pass an integer depth so Lodash stops before the structure is fully linear. That is ideal when you know your payloads are “at most two levels of arrays” or when you want to preserve one intentional inner list.
Bounded work
Unlike full recursion, worst-case work scales with depth you choose, not unknown tree height.
Default depth 1
Omitting depth behaves like a single _.flatten pass for ordinary nested arrays.
New array
The source array is not mutated; you always receive a new result reference.
Syntax
_.flattenDepth(array, [depth=1]) - array: collection that may contain nested arrays (array-like values are coerced).
- depth: how many levels of nested arrays to flatten (non-negative integer; defaults to
1). - Returns: a new array with nesting reduced by up to
depthlevels.
Two levels of unwrapping
With depth: 2, two layers of brackets disappear; a third layer can remain.
import flattenDepth from "lodash/flattenDepth";
flattenDepth([1, [2, [3, [4]]]], 2);
// [1, 2, 3, [4]] Default depth equals one
Omitting the second argument matches a single shallow flatten pass.
import flattenDepth from "lodash/flattenDepth";
flattenDepth([[1, 2], [3, [4, 5]]]);
// [1, 2, 3, [4, 5]] Same idea as native flat
For a plain array instance, flat(2) and flattenDepth(arr, 2) express the same depth budget.
import flattenDepth from "lodash/flattenDepth";
const nested = [0, [1, [2, [3]]]];
flattenDepth(nested, 2);
// [0, 1, 2, [3]]
nested.flat(2);
// [0, 1, 2, [3]] 📋 _.flattenDepth vs _.flatten vs _.flattenDeep
| API | Levels removed | When to pick it |
|---|---|---|
_.flatten(array) | Always 1 | Known single wrapper around batches |
_.flattenDepth(array, n) | Up to n | Bounded depth or preserving one inner list on purpose |
_.flattenDeep(array) | All | Unknown or variable nesting; must end with no arrays |
Pitfalls to avoid
Off-by-one nesting
If your API adds one more wrapper in a new version, a fixed depth may stop early. Add tests around real payloads.
Only real arrays unwrap
Array-like objects and plain objects are not recursively opened the way you might expect from custom trees.
Still not a deep clone
Leaf objects and arrays that survive flattening share references with the source.
❓ FAQ
Summary
- Purpose:
_.flattenDepth(array, depth)returns a new array with up todepthlevels of nesting removed (defaultdepthis1). - Safety: the input array is not mutated.
- Next: Lodash _.fromPairs(), _.flattenDeep() (earlier in this track), or the array methods hub.
With depth set to 1, _.flattenDepth matches _.flatten for typical arrays: both unwrap a single bracket layer.
6 people found this page helpful
