Lodash _.flatten() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Flattens array a single level deep and returns a new array. Deeper nests stay nested. Part of Lodash 4.x Array (since 0.1.0).

Note: Native Array.prototype.flat(1) (ES2019) does the same one-level job. Prefer Lodash when you already ship Lodash or need consistent imports across older targets.

Syntax

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

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

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

Library declarations
Lodash
_.flatten(array)

Parameters

ParameterDescription
arrayRequired. Source to flatten one level

Return Value

Return ValueDescription
*New flattened array

Lodash Examples

Example 1 — Official: One Level Only

Deeper nests remain after a single flatten.

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

How It Works

  1. Source. Top level has 1 and one nested array.
  2. Unwrap once. That nested array expands to 2, [3,[4]], 5.
  3. Stop. [3,[4]] is still an array element — not unwrapped again.
  4. Rule. Shallow flatten ≠ recursive flatten.

Example 2 — One-Level Pairs

When every nest is only one deep, flatten fully merges them.

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

How It Works

  1. Two groups. Each child array is one level deep.
  2. First unwrap. [1, 2] → 1, 2.
  3. Second unwrap. [3, 4] → 3, 4.
  4. Result. A single flat list — common after grouping or mapping to arrays.

Example 3 — Already Flat

No nested arrays — values copy into a new array.

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

How It Works

  1. No nests. Every item is a plain value.
  2. Copy. Lodash still builds a new array.
  3. Safe. Calling flatten on flat data is a no-op for shape.
  4. Tip. Do not rely on reference equality with the input.

Example 4 — Empty Array

Flattening nothing yields an empty array.

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

How It Works

  1. Empty source. Nothing to unwrap.
  2. Result. [] — no throw.
  3. Safe default. Easy to chain after filters that may return empty.
  4. Guard. Check result.length if you need at least one item.

Example 5 — Mixed Non-Array Values

Scalars and null pass through; only arrays unwrap.

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

How It Works

  1. Scalars stay. 1 and null are not arrays.
  2. Unwrap nests. [2] and [3] contribute their elements.
  3. Order preserved. Left-to-right, including null.
  4. Tip. Use _.compact afterward if you want falsy values removed.

Browser & Runtime Support

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

✓ Stable · Lodash 4.x

_.flatten()

Available in [email protected] full builds and the lodash/flatten modular import. Native Array.prototype.flat 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
_.flatten Lodash 4.x+

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

Frequently Asked Questions

_.flatten(array) creates a new array with nested arrays unwrapped one level deep. Deeper nesting stays nested. The source array is not mutated.
No. It returns a new array. Elements are copied into the result in order; object references inside are still shared.
Exactly one. For full recursion use _.flattenDeep. For a chosen depth use _.flattenDepth(array, depth).
Behavior for one-level flatten is the same idea. Lodash _.flatten helps when you already use Lodash imports or need older environments without Array.prototype.flat.
Use import flatten from "lodash/flatten" or require("lodash/flatten") for tree-shaking friendly bundles. Full build: import _ from "lodash" then _.flatten.
_.flatten([]) returns []. _.flatten([1, 2, 3]) returns a new [1, 2, 3] — same values, still a new array.

Next: _.flattenDeep()

Recursively flatten every nesting level into one list.

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