Lodash _.flatMapDepth() method
What you’ll learn
- How
_.flatMapDepth(collection, iteratee, depth)blends mapping with capped flattening. - Choosing depth when iteratees emit predictable nesting (two bursts vs fully recursive trees).
- How this sits between
_.flatMap(depth 1) and_.flatMapDeep(unbounded). - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Skim _.flatMap() and _.flatMapDeep() so the depth knob has clear endpoints.
- You can visualize nested arrays as layers to peel.
- You can open Try-it labs or run snippets locally.
Overview
_.flatMapDepth is the Goldilocks helper—enough flatten passes to normalize awkward payloads without blindly collapsing structures that should stay grouped.
Explicit depth
Dial in two or three passes when schemas wrap rows in predictable wrappers.
Lodash iteratees
Reuse matchers and property shorthand exactly like flatMap siblings.
Safer than infinite
Preserves deliberate inner arrays once depth budget runs out.
Syntax
_.flatMapDepth(collection, iteratee, depth) - collection: array, array-like, or plain object Lodash can iterate.
- iteratee: invoked as
(value, index|key, collection); results concatenate before flattening. - depth: non-negative integer controlling how many flatten passes run (behavior aligns with
_.flattenDepth). - Returns: new array flattened up to
depthafter mapping.
Flatten two levels after mapping
Each number emits a value plus a nested singleton—two flatten passes unwrap both layers.
import flatMapDepth from "lodash/flatMapDepth";
flatMapDepth([2, 4], (n) => [n / 2, [n]], 2);
// → [1, 2, 2, 4] Depth 1 matches standard flatMap
Each iteratee returns a small tuple—one flatten pass merges those tuples, matching _.flatMap.
import flatMapDepth from "lodash/flatMapDepth";
flatMapDepth([1, 2], (n) => [n, n * 10], 1);
// → [1, 10, 2, 20] Bounded flatten on object values
Iterate values from keyed records—depth 2 opens grouped batches without erasing intentional deeper tuples beyond that budget.
import flatMapDepth from "lodash/flatMapDepth";
flatMapDepth(
{ east: [[1, 2]], west: [[3, 4]] },
(chunk) => chunk,
2
);
// → [1, 2, 3, 4] 📋 _.flatMapDepth vs flatMap, flatMapDeep, flattenDepth
| API | Flattening | Best when |
|---|---|---|
_.flatMapDepth(collection, iteratee, depth) | Up to depth levels | You know how many wrappers to peel |
_.flatMap(collection, iteratee) | Exactly one level | Simple parent→children bursts |
_.flatMapDeep(collection, iteratee) | Fully recursive | Unknown or highly variable nesting |
_.flattenDepth(array, depth) | Depth only—no map | Already mapped elsewhere |
Pitfalls to avoid
Off-by-one nesting
Too shallow leaves stray brackets; too deep eats intentional grouping—snapshot fixtures when tuning.
Argument order
Depth trails iteratee—mirror Lodash docs when copying snippets.
Repeated flatten passes
Large batches still allocate intermediate arrays—measure before micro-optimizing elsewhere.
❓ FAQ
Summary
- Purpose:
_.flatMapDepth(collection, iteratee, depth)maps then flattens up to a capped depth. - Contrast: bump to
flatMapDeeponly when recursion truly must run to completion. - Next: Lodash _.forEach(), Lodash _.flatMapDeep() (previous), or collection hub.
Depth 1 matches _.flatMap; very large depths approximate _.flatMapDeep—pick the smallest depth that still produces the shape your downstream code expects.
6 people found this page helpful
