Lodash _.flatMapDeep() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

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-hoc map + 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

javascript
_.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.
1

Mix scalars with nested arrays per element

_.flatMap would leave the inner arrays intact; flatMapDeep collapses them into one plane.

javascript
import flatMapDeep from "lodash/flatMapDeep";

flatMapDeep([1, 2], (n) => [n, [n + 10]]);
// → [1, 11, 2, 12]
Try it Yourself
2

Triple-nested iteratee output

Useful when scaffolding tests or fixtures where helpers wrap values multiple times before normalization.

javascript
import flatMapDeep from "lodash/flatMapDeep";

flatMapDeep([10, 20], (x) => [[[x, x + 1]]]);
// → [10, 11, 20, 21]
Try it Yourself
3

Flatten mapped values from an object

Keyed collections still flow through the same helper—outputs concatenate then flatten deeply.

javascript
import flatMapDeep from "lodash/flatMapDeep";

flatMapDeep(
  { a: [1, [2]], b: [[3, 4]] },
  (vals) => vals
);
// → [1, 2, 3, 4]
Try it Yourself

📋 _.flatMapDeep vs flatMap, flatMapDepth, map + flattenDeep

APIFlatteningBest when
_.flatMapDeep(collection, iteratee)RecursiveIteratee emits unknown nesting depth
_.flatMap(collection, iteratee)Single levelOne burst of child rows per parent
_.flatMapDepth(collection, iteratee, depth)BoundedYou know max depth but it is > 1
_.flattenDeep(_.map(...))RecursiveEquivalent composition—pick whichever reads clearer

Pitfalls to avoid

Size

Combinatorial blowups

Each expansion multiplies output length—profile real payloads before shipping.

Depth

Runaway nesting

Malicious or buggy iteratees can churn enormous stacks; prefer bounded helpers when inputs are untrusted.

Clarity

Opaque pipelines

Sometimes explicit map then named flatten passes communicates intent better than a single mega-call.

❓ FAQ

No. Lodash allocates a new array after mapping and recursive flattening; sources stay unchanged.
flatMap flattens only one level after mapping; flatMapDeep keeps flattening until no nested arrays remain in the iteratee output.
Yes—scalar outputs are appended as single elements; arrays get flattened recursively.
Yes—Lodash maps values first, then flattens the combined results deeply.
flatMapDepth lets you cap flatten recursion—a middle ground between flatMap and flatMapDeep.

Summary

Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful