Lodash _.flatMapDeep() method
What you’ll learn
- How
_.flatMapDeep(collection, iteratee)merges mapping with recursive flattening. - When deeply nested iteratee outputs are intentional—and when capped-depth helpers are safer.
- How this compares to
_.flatMap,_.flatMapDepth, and ad-hocmap+flattenDeep. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Read _.flatMap() first—this tutorial turns the flatten knob from depth one to recursive.
- You understand nested arrays and why recursion can balloon output size.
- You can open Try-it labs or run snippets locally.
Overview
_.flatMapDeep shines when iteratees naturally emit trees—CSV rows exploding into tokens, GraphQL edges unfolding into leaves, or legacy APIs returning arbitrarily nested tuples.
Recursive flatten
Keeps collapsing nested arrays produced after mapping until only atoms remain.
Same iteratees
Property paths, matcher objects, and functions behave like other Lodash collection helpers.
Power trade-off
Convenient but eager—watch payload sizes before dropping into hot paths.
Syntax
_.flatMapDeep(collection, iteratee) - collection: array, array-like, or plain object Lodash can iterate.
- iteratee: invoked as
(value, index|key, collection); outputs may be scalars or arbitrarily nested arrays. - Returns: new array with all nested arrays flattened after mapping.
Mix scalars with nested arrays per element
_.flatMap would leave the inner arrays intact; flatMapDeep collapses them into one plane.
import flatMapDeep from "lodash/flatMapDeep";
flatMapDeep([1, 2], (n) => [n, [n + 10]]);
// → [1, 11, 2, 12] Triple-nested iteratee output
Useful when scaffolding tests or fixtures where helpers wrap values multiple times before normalization.
import flatMapDeep from "lodash/flatMapDeep";
flatMapDeep([10, 20], (x) => [[[x, x + 1]]]);
// → [10, 11, 20, 21] Flatten mapped values from an object
Keyed collections still flow through the same helper—outputs concatenate then flatten deeply.
import flatMapDeep from "lodash/flatMapDeep";
flatMapDeep(
{ a: [1, [2]], b: [[3, 4]] },
(vals) => vals
);
// → [1, 2, 3, 4] 📋 _.flatMapDeep vs flatMap, flatMapDepth, map + flattenDeep
| API | Flattening | Best when |
|---|---|---|
_.flatMapDeep(collection, iteratee) | Recursive | Iteratee emits unknown nesting depth |
_.flatMap(collection, iteratee) | Single level | One burst of child rows per parent |
_.flatMapDepth(collection, iteratee, depth) | Bounded | You know max depth but it is > 1 |
_.flattenDeep(_.map(...)) | Recursive | Equivalent composition—pick whichever reads clearer |
Pitfalls to avoid
Combinatorial blowups
Each expansion multiplies output length—profile real payloads before shipping.
Runaway nesting
Malicious or buggy iteratees can churn enormous stacks; prefer bounded helpers when inputs are untrusted.
Opaque pipelines
Sometimes explicit map then named flatten passes communicates intent better than a single mega-call.
❓ FAQ
Summary
- Purpose:
_.flatMapDeep(collection, iteratee)maps then recursively flattens nested arrays in the results. - Contrast: downgrade to
_.flatMapor_.flatMapDepthwhen you can cap nesting. - Next: Lodash _.flatMapDepth(), Lodash _.flatMap() (previous), or collection hub.
When you only need a fixed number of flatten passes instead of “all the way down,” prefer _.flatMapDepth (or compose _.flatMap) so accidental mega-arrays are less likely.
6 people found this page helpful
