Lodash _.flatMapDepth() method

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

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

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

Flatten two levels after mapping

Each number emits a value plus a nested singleton—two flatten passes unwrap both layers.

javascript
import flatMapDepth from "lodash/flatMapDepth";

flatMapDepth([2, 4], (n) => [n / 2, [n]], 2);
// → [1, 2, 2, 4]
Try it Yourself
2

Depth 1 matches standard flatMap

Each iteratee returns a small tuple—one flatten pass merges those tuples, matching _.flatMap.

javascript
import flatMapDepth from "lodash/flatMapDepth";

flatMapDepth([1, 2], (n) => [n, n * 10], 1);
// → [1, 10, 2, 20]
Try it Yourself
3

Bounded flatten on object values

Iterate values from keyed records—depth 2 opens grouped batches without erasing intentional deeper tuples beyond that budget.

javascript
import flatMapDepth from "lodash/flatMapDepth";

flatMapDepth(
  { east: [[1, 2]], west: [[3, 4]] },
  (chunk) => chunk,
  2
);
// → [1, 2, 3, 4]
Try it Yourself

📋 _.flatMapDepth vs flatMap, flatMapDeep, flattenDepth

APIFlatteningBest when
_.flatMapDepth(collection, iteratee, depth)Up to depth levelsYou know how many wrappers to peel
_.flatMap(collection, iteratee)Exactly one levelSimple parent→children bursts
_.flatMapDeep(collection, iteratee)Fully recursiveUnknown or highly variable nesting
_.flattenDepth(array, depth)Depth only—no mapAlready mapped elsewhere

Pitfalls to avoid

Depth

Off-by-one nesting

Too shallow leaves stray brackets; too deep eats intentional grouping—snapshot fixtures when tuning.

API

Argument order

Depth trails iteratee—mirror Lodash docs when copying snippets.

Perf

Repeated flatten passes

Large batches still allocate intermediate arrays—measure before micro-optimizing elsewhere.

❓ FAQ

No. Lodash returns a new array after mapping and bounded flattening; originals stay unchanged.
It is the number of flatten passes applied to the concatenated iteratee outputs—same idea as _.flattenDepth on arrays.
flatMapDeep flattens all the way down; flatMapDepth stops after a fixed number of levels.
Lodash treats depth like flattenDepth—0 yields essentially no flattening of nested arrays (only mapping semantics apply).
Yes—values flow through the same iteratee pipeline before bounded flattening.

Summary

Did you know?

Depth 1 matches _.flatMap; very large depths approximate _.flatMapDeep—pick the smallest depth that still produces the shape your downstream code expects.

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