Lodash _.flatMapDepth() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Collection

Definition

Like _.flatMap, but flattens mapped results up to a chosen depth (default 1). Middle ground between flatMap and _.flatMapDeep. Part of Lodash 4.x Collection (since 4.7.0).

Pass the required arguments shown below. Lodash Collection helpers usually return a new value — check whether the source is mutated.

Syntax

Call _.flatMapDepth on a collection. Start with this line.

Lodash
_.flatMapDepth(collection, [iteratee], [depth=1])
CallWhat it does
_.flatMapDepth(collection, [iteratee], [depth=1])Primary signature.
_.flatMapDepth(collection)Call with a collection argument.

Prefer import flatMapDepth from "lodash/flatMapDepth" when you want a smaller bundle.

Library declarations
Lodash
_.flatMapDepth(collection, [iteratee], [depth=1])

Parameters

ParameterDescription
collectionRequired. Array or object to iterate
iterateeOptional. Maps each value; default _.identity
depthOptional. Max flatten levels; default 1

Return Value

Return ValueDescription
*New array flattened up to depth

Lodash Examples

Example 1 — Official: Depth 2

Triple-wrap each number; peel two layers.

Lodash
function duplicate(n) {
  return [[[n, n]]];
}

_.flatMapDepth([1, 2], duplicate, 2);
// → [[1, 1], [2, 2]]
Try It Yourself

How It Works

  1. Iteratee. Each value becomes [[[n, n]]] — three layers.
  2. Map. Builds a nested array of those wrappers.
  3. Depth 2. Two flatten passes leave the inner pairs.
  4. Result. [[1,1],[2,2]] — not fully flat yet.

Example 2 — Depth 1 Matches _.flatMap

Default / depth 1 concatenates one level of iteratee arrays.

Lodash
_.flatMapDepth([1, 2], function (n) {
  return [n, n * 10];
}, 1);
// → [1, 10, 2, 20]

_.flatMap([1, 2], function (n) {
  return [n, n * 10];
});
// → [1, 10, 2, 20]
Try It Yourself

How It Works

  1. Pairs. Each number yields [n, n*10].
  2. One flatten. Pairs concatenate into one list.
  3. Same as flatMap. Depth 1 is the flatMap case.
  4. Prefer. Use _.flatMap when depth is always one — clearer intent.

Example 3 — Depth Ladder on Triple Wrap

Same iteratee; raise depth until leaves appear.

Lodash
function duplicate(n) {
  return [[[n, n]]];
}

_.flatMapDepth([1, 2], duplicate, 1);
// → [[[1, 1]], [[2, 2]]]

_.flatMapDepth([1, 2], duplicate, 2);
// → [[1, 1], [2, 2]]

_.flatMapDepth([1, 2], duplicate, 3);
// → [1, 1, 2, 2]
Try It Yourself

How It Works

  1. Depth 1. One layer off — still double-wrapped pairs.
  2. Depth 2. Official result — single-wrapped pairs.
  3. Depth 3. Fully flat — matches _.flatMapDeep here.
  4. Rule. Pick the smallest depth that yields the shape you need.

Example 4 — Mixed Nesting With Depth 2

Scalars and nested arrays in one iteratee return value.

Lodash
_.flatMapDepth([2, 4], function (n) {
  return [n / 2, [n]];
}, 2);
// → [1, 2, 2, 4]
Try It Yourself

How It Works

  1. For 2. Returns [1, [2]].
  2. For 4. Returns [2, [4]].
  3. Depth 2. Enough to unwrap the inner [n] arrays.
  4. Leaves. Final list is [1, 2, 2, 4].

Example 5 — Object Values With Depth 2

Plain-object collections map values, then flatten to the chosen depth.

Lodash
_.flatMapDepth(
  { east: [[1, 2]], west: [[3, 4]] },
  function (chunk) {
    return chunk;
  },
  2
);
// → [1, 2, 3, 4]
Try It Yourself

How It Works

  1. Values. east → [[1,2]]; west → [[3,4]].
  2. Map. Produces nested regional batches.
  3. Depth 2. Unwraps both outer wrappers to numbers.
  4. Order. Follows Lodash object enumeration order.

Browser & Runtime Support

_.flatMapDepth is a Lodash 4.x Collection helper (added in 4.7.0). It runs anywhere Lodash 4.7+ runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native Array.flatMap only flattens one level; arr.flat(depth) has no map step.

✓ Stable · Lodash 4.7+

_.flatMapDepth()

Available in [email protected]+ full builds and the lodash/flatMapDepth modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.7+
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.flatMapDepth Lodash 4.7+

Bottom line: Safe for production on Lodash 4.7+. Prefer import flatMapDepth from "lodash/flatMapDepth" for smaller builds. For native code, compose map + flat(depth).

Frequently Asked Questions

_.flatMapDepth(collection, iteratee, depth) maps each element through iteratee, then flattens the mapped results up to depth levels. Default depth is 1 (same idea as _.flatMap).
No. It returns a new array. The source collection is left unchanged.
flatMapDeep flattens all the way. flatMapDepth stops after a fixed number of flatten passes you choose.
1 — so _.flatMapDepth(col, fn) matches _.flatMap(col, fn) when depth is omitted.
Yes. Depth 0 means no flattening after the map step — nested arrays from the iteratee stay nested (like _.map outputting arrays).
_.flatMapDepth was added in Lodash 4.7.0. It is stable in Lodash 4.x.

Next: _.forEach()

Iterate a collection for side effects — Lodash’s each helper.

_.forEach() tutorial →

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