Lodash _.flatten() method

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

What you’ll learn

  • How _.flatten(array) produces a new one-level-flatter array.
  • Why double-nested arrays still contain an inner array after a single flatten pass.
  • How this compares to Array.prototype.flat(1) and when to reach for _.flattenDeep.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Optional context: _.chunk() builds nested arrays; _.findLastIndex() sits earlier on the same tutorial track.

  • You know that nested arrays are still normal values at the outer index until you unwrap them.
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.flatten is the shallow un-nester: every element that is itself an array becomes its children in order, one time only. It is the right default when you control the shape (for example API batches that are at most one array deep) and want predictable performance.

One level

Perfect when you only need to peel a single wrapper, not arbitrary tree depth.

New array

The input array is unchanged; the result is a fresh linear sequence of references.

Pairs with chunk

A common pattern is chunk then later flatten when you are done grouping.

Syntax

javascript
_.flatten(array)
  • array: collection whose top-level elements may be arrays (array-like values are coerced).
  • Returns: a new array where each top-level array element is replaced by its contents, in order. Non-array elements are copied through unchanged.
1

Typical nested batch

Two inner arrays become one sequence preserving left-to-right order.

javascript
import flatten from "lodash/flatten";

flatten([[1, 2], [3, 4]]);
// [1, 2, 3, 4]
Try it Yourself
2

Still nested after one pass

The innermost [3] started two levels deep; after flatten once you still have an array at index 2.

javascript
import flatten from "lodash/flatten";

flatten([1, [2, [3]]]);
// [1, 2, [3]]
Try it Yourself
3

Mix of values and arrays

Scalars pass through; only array elements expand into the result.

javascript
import flatten from "lodash/flatten";

flatten(["a", ["b", "c"], "d"]);
// ["a", "b", "c", "d"]
Try it Yourself

📋 _.flatten vs native flat(1) vs deeper helpers

APIDepthNote
_.flatten(array)One levelLodash module import; consistent on array-like inputs
array.flat(1)One levelNative ES2019; same idea for real arrays
_.flattenDeep / _.flattenDepthAll / configurableUse when nesting depth is unknown or greater than one

Pitfalls to avoid

Depth

Assuming full recursion

User-generated trees often need flattenDeep or an explicit loop; a single flatten leaves surprises one level down.

Objects

Not a deep clone

Flatten copies references. Nested objects inside arrays are not duplicated; mutating them still affects every alias.

Sparse

Holes and undefined slots

Behavior follows Lodash collection rules; when in doubt, log the output for sparse arrays before shipping.

❓ FAQ

No. Lodash returns a new array. Nested inner arrays in the source are not modified; their elements are copied into the new result in order.
Exactly one level of array nesting. Deeper arrays stay wrapped until you call flatten again, use flattenDepth with a higher depth, or use flattenDeep for full recursion.
concat appends whole arguments as elements (unless you spread them). flatten unwraps only the top level of nested arrays inside a single input array.
Array.prototype.flat(1) flattens one level similarly. Lodash flatten matches the rest of the Lodash import style and older environments without flat.
Use import flatten from "lodash/flatten" or require("lodash/flatten") for tree-shaking friendly bundles.

Summary

Did you know?

_.flatten is intentionally shallow: nested arrays deeper than one level remain nested unless you switch to _.flattenDeep or _.flattenDepth.

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