Lodash _.flatMap() method
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+concatand when to escalate toflatMapDeep. - 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
_.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.
Flatten nested role arrays
Each user contributes an array of capability strings—flatMap yields a single merged roster.
import flatMap from "lodash/flatMap";
flatMap(
[
{ name: "Ada", scopes: ["read", "write"] },
{ name: "Lin", scopes: ["read"] }
],
(user) => user.scopes
);
// → ["read", "write", "read"] One-to-many numeric projection
Iteratees may emit multiple scalars per input—flatMap preserves ordering within each expansion.
import flatMap from "lodash/flatMap";
flatMap([1, 2, 3], (n) => [n, n * 10]);
// → [1, 10, 2, 20, 3, 30] 📋 _.flatMap vs map, flatten, native flatMap
| API | Flatten depth | Best when |
|---|---|---|
_.flatMap(collection, iteratee) | One level after map | One-to-many expansions on Lodash collections |
_.map + _.flatten | One level (explicit) | You want visibly separated stages |
_.flatMapDeep | Recursive | Arbitrarily nested iteratee outputs |
array.flatMap(iteratee) | One level | Pure arrays in modern runtimes |
Pitfalls to avoid
Still nested?
Double-nested arrays survive flatMap—either flatten again or upgrade to flatMapDeep with clear intent.
Unwanted repeats
flatMap preserves duplicates coming from each branch—dedupe downstream if uniqueness matters.
Huge intermediates
Exploding large graphs into giant arrays can spike memory—stream or chunk when datasets grow.
❓ FAQ
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.
_.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.
6 people found this page helpful
