Lodash _.flattenDeep() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Recursively flattens array until every element is non-array. Returns a new one-dimensional list. Part of Lodash 4.x Array (since 3.0.0).

Note: Native Array.prototype.flat(Infinity) (ES2019) is a close twin. Prefer Lodash when you already ship Lodash or need consistent imports on older targets.

Syntax

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

Lodash
_.flattenDeep(array)
CallWhat it does
_.flattenDeep(array)Primary signature.
_.flattenDeep(array)Call with an array argument.

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

Library declarations
Lodash
_.flattenDeep(array)

Parameters

ParameterDescription
arrayRequired. Source to flatten recursively

Return Value

Return ValueDescription
*New fully flattened array

Lodash Examples

Example 1 — Official: Fully Flat

All nesting levels unwrap into one list.

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

How It Works

  1. Source. Same nested shape as the official _.flatten demo.
  2. Recurse. Every array child is flattened again.
  3. Leaves. 1, 2, 3, 4, 5 are non-arrays — they stay.
  4. Contrast. Shallow _.flatten would leave [3,[4]] nested.

Example 2 — Mixed Nest Depths

Different branches can nest to different depths.

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

How It Works

  1. First branch. [1, 2] unwraps to 1, 2.
  2. Second branch. [3, [4]] unwraps 3, then recurses into [4].
  3. Order. Left-to-right leaf order is preserved.
  4. Use. Handy after mapping that returns uneven nests.

Example 3 — Deeply Wrapped Value

Many empty layers still collapse to the single leaf.

Lodash
_.flattenDeep([[[[5]]]]);
// → [5]
Try It Yourself

How It Works

  1. Four wraps. 5 sits inside four array layers.
  2. Recurse. Each call peels one layer.
  3. Result. A one-element array holding 5.
  4. Tip. Empty intermediate arrays contribute no extra values.

Example 4 — Already Flat

No nested arrays — values copy into a new array.

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

How It Works

  1. No nests. Every item is already a leaf.
  2. Copy. Lodash still builds a new array.
  3. Safe. Shape stays the same when input is flat.
  4. Tip. Do not rely on reference equality with the input.

Example 5 — Mixed Non-Array Values

Scalars and null pass through; only arrays recurse.

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

How It Works

  1. Scalars stay. 1 and null are not arrays.
  2. Unwrap nests. [2] and [3,[4]] recurse to leaves.
  3. Order preserved. Including null in place.
  4. Tip. Chain _.compact if you want falsy values removed.

Browser & Runtime Support

_.flattenDeep is a Lodash 4.x Array helper (since 3.0.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.x

_.flattenDeep()

Available in [email protected] full builds and the lodash/flattenDeep modular import. Native Array.prototype.flat(Infinity) needs ES2019 (Chrome 69+, Firefox 62+, Safari 12+, Node 11+).

100% With Lodash 4.x
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
_.flattenDeep Lodash 4.x+

Bottom line: Safe for production once Lodash is loaded. Prefer import flattenDeep from "lodash/flattenDeep" for smaller bundles. On modern-only targets, arr.flat(Infinity) is a fine native alternative.

Frequently Asked Questions

_.flattenDeep(array) recursively flattens nested arrays until every element is non-array. It returns a new one-dimensional array and does not mutate the source.
No. Lodash walks the nest and builds a brand-new flat array. The source structure is left unchanged.
_.flatten removes only one nesting level. _.flattenDeep keeps unwrapping until no element is an array.
_.flattenDepth(array, n) stops after n levels. flattenDeep is like unbounded depth — keep going until fully flat.
Array.prototype.flat(Infinity) is similar on ES2019+. Lodash helps with consistent imports and older environments without flat.
Use import flattenDeep from "lodash/flattenDeep" or require("lodash/flattenDeep") for tree-shaking friendly bundles. Full build: import _ from "lodash" then _.flattenDeep.

Next: _.flattenDepth()

Control how many nesting levels to unwrap.

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