Lodash _.flatMap() method

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

What you’ll learn

  • How _.flatMap(collection, iteratee) fuses mapping with single-depth flattening.
  • Patterns for one-to-many projections (tokens, permissions, child rows).
  • How Lodash differs from bare map + concat and when to escalate to flatMapDeep.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Comfort with _.filter() and _.find() helps—you are still thinking in collection iteratees.

  • You understand arrays nested one level inside other arrays.
  • You can open Try-it labs or run snippets locally.

Overview

_.flatMap removes the boilerplate of pushing spread results after every map—ideal when each row expands into zero or more contributions but you still want a single flat list afterward.

Map + flatten once

Combines two common transforms without manual reduce loops.

Objects too

Iterate values from keyed collections using familiar Lodash iteratees.

Predictable depth

Exactly one flatten level keeps surprises smaller than recursive helpers.

Syntax

javascript
_.flatMap(collection, iteratee)
  • collection: array, array-like, or plain object Lodash can iterate.
  • iteratee: invoked as (value, index|key, collection); return arrays or scalar values—arrays concatenate, scalars append as single entries.
  • Returns: new array with one level of nesting removed after mapping.
1

Explode strings into tokens

Each source string maps to multiple words; flatMap stitches them into one continuous list.

javascript
import flatMap from "lodash/flatMap";

flatMap(["alpha beta", "gamma"], (chunk) => chunk.split(" "));
// → ["alpha", "beta", "gamma"]
Try it Yourself
2

Flatten nested role arrays

Each user contributes an array of capability strings—flatMap yields a single merged roster.

javascript
import flatMap from "lodash/flatMap";

flatMap(
  [
    { name: "Ada", scopes: ["read", "write"] },
    { name: "Lin", scopes: ["read"] }
  ],
  (user) => user.scopes
);
// → ["read", "write", "read"]
Try it Yourself
3

One-to-many numeric projection

Iteratees may emit multiple scalars per input—flatMap preserves ordering within each expansion.

javascript
import flatMap from "lodash/flatMap";

flatMap([1, 2, 3], (n) => [n, n * 10]);
// → [1, 10, 2, 20, 3, 30]
Try it Yourself

📋 _.flatMap vs map, flatten, native flatMap

APIFlatten depthBest when
_.flatMap(collection, iteratee)One level after mapOne-to-many expansions on Lodash collections
_.map + _.flattenOne level (explicit)You want visibly separated stages
_.flatMapDeepRecursiveArbitrarily nested iteratee outputs
array.flatMap(iteratee)One levelPure arrays in modern runtimes

Pitfalls to avoid

Depth

Still nested?

Double-nested arrays survive flatMap—either flatten again or upgrade to flatMapDeep with clear intent.

Dupes

Unwanted repeats

flatMap preserves duplicates coming from each branch—dedupe downstream if uniqueness matters.

Perf

Huge intermediates

Exploding large graphs into giant arrays can spike memory—stream or chunk when datasets grow.

❓ FAQ

No. Lodash builds a brand-new flattened array; inputs remain untouched.
Exactly one level—nested arrays inside iteratee results stay nested unless you switch helpers.
map preserves one output per element; flatMap expects iteratee outputs that may themselves be arrays (or array-like) and concatenates them.
Yes—it walks values and flattens iteratee outputs in collection order.
Lodash adds object collections and iteratee shorthands; on arrays the mental model matches ES2019 flatMap.

Summary

  • Purpose: _.flatMap(collection, iteratee) maps then flattens one level into a new array.
  • Contrast: prefer chained map/flatten when staging aids readability; reach for deep helpers when recursion is required.
  • Next: Lodash _.flatMapDeep(), Lodash _.findLast() (previous), or collection hub.
Did you know?

_.flatMap is equivalent to _.map followed by _.flatten with depth one. When nested arrays must collapse entirely, reach for _.flatMapDeep or tune depth with _.flatMapDepth.

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