Lodash _.flattenDepth() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Recursively flattens array up to depth times. Deeper nesting than depth can remain. Part of Lodash 4.x Array (stable since 4.4.0).

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

Syntax

Call _.flattenDepth on an array. Start with this line.

Lodash
_.flattenDepth(array, [depth=1])
CallWhat it does
_.flattenDepth(array, [depth=1])Primary signature.
_.flattenDepth(array)Call with an array argument.

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

Library declarations
Lodash
_.flattenDepth(array, [depth=1])

Parameters

ParameterDescription
arrayRequired. Source that may nest arrays
depthOptional. Max levels to flatten; default 1

Return Value

Return ValueDescription
*New array with nesting reduced

Lodash Examples

Example 1 — Official: Depth 1

One level of nesting is removed; deeper arrays remain.

Lodash
var array = [1, [2, [3, [4]], 5]];

_.flattenDepth(array, 1);
// → [1, 2, [3, [4]], 5]
Try It Yourself

How It Works

  1. Source. Official sample: [1, [2, [3, [4]], 5]].
  2. Budget 1. Only the outer wrapper around 2 and the nested list is opened once.
  3. Still nested. [3, [4]] survives because depth ran out.
  4. Order. Leaves stay in depth-first order: 1, 2, …, 5.

Example 2 — Official: Depth 2

Two peels leave only the innermost [4].

Lodash
var array = [1, [2, [3, [4]], 5]];

_.flattenDepth(array, 2);
// → [1, 2, 3, [4], 5]
Try It Yourself

How It Works

  1. First peel. Opens [2, [3, [4]]].
  2. Second peel. Opens [3, [4]] → 3 and [4].
  3. Stop. Budget is spent, so [4] stays an array.
  4. vs flattenDeep. _.flattenDeep would also unwrap [4] → 4.

Example 3 — Default Depth 1

Omit the second argument and Lodash uses depth 1.

Lodash
_.flattenDepth([[1, 2], [3, [4, 5]]]);
// → [1, 2, 3, [4, 5]]
Try It Yourself

How It Works

  1. Default. Missing depth means 1.
  2. Outer groups. [1, 2] and [3, [4, 5]] unwrap once.
  3. Inner list. [4, 5] is still an array after one peel.
  4. Same idea. Matches _.flatten for this shape.

Example 4 — Depth 0

Zero means “copy structure, do not flatten.”

Lodash
_.flattenDepth([1, [2, [3]]], 0);
// → [1, [2, [3]]]
Try It Yourself

How It Works

  1. Budget 0. Nested arrays are never opened.
  2. New array. You still get a new top-level array (shallow copy of slots).
  3. Shared inners. Nested arrays are the same references as in the source.
  4. Use. Handy when an API always wants flattenDepth but sometimes depth is zero.

Example 5 — Objects Are Not Flattened

Only arrays unwrap. Plain objects stay as single values.

Lodash
_.flattenDepth([{ id: 1 }, [{ id: 2 }]], 2);
// → [{ id: 1 }, { id: 2 }]
Try It Yourself

How It Works

  1. First value. { id: 1 } is an object → kept as-is.
  2. Nested array. [{ id: 2 }] unwraps because depth allows it.
  3. Leaf object. { id: 2 } is not an array → not “opened” further.
  4. Rule. Flattening is about array wrappers, not object trees.

Browser & Runtime Support

_.flattenDepth is a Lodash 4.x Array helper (added in 4.4.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.4.0+

_.flattenDepth()

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

100% With Lodash 4.4.0+
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
_.flattenDepth Lodash 4.4.0+

Bottom line: Safe for production once Lodash 4.4.0+ is on the page or in your bundle. Prefer import flattenDepth from "lodash/flattenDepth" for smaller builds. Native alternative on real arrays: Array.prototype.flat(depth) (ES2019).

Frequently Asked Questions

_.flattenDepth(array, depth) recursively flattens nested arrays up to depth times and returns a new array. Deeper nesting than depth can remain.
When depth is omitted, Lodash defaults to 1 — the same idea as _.flatten for ordinary nested arrays.
flattenDeep keeps unwrapping until no arrays remain. flattenDepth stops after exactly the depth you pass, which may leave nested arrays in the result.
No. It returns a new array. Leaf objects and any arrays that survive flattening still share references with the source.
A depth of 0 returns a shallow copy of the array without removing any nesting — useful when you want the same API but no flattening.
Use import flattenDepth from "lodash/flattenDepth" or require("lodash/flattenDepth") for tree-shaking friendly bundles. Full build: import _ from "lodash" then _.flattenDepth.

Next: _.fromPairs()

Turn an array of key-value pairs into a plain object.

_.fromPairs() 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.

10 people found this page helpful