Lodash _.flatMap() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Collection

Definition

Runs each element through an iteratee, then flattens the mapped results one level into a new array. Same idea as _.map + _.flatten. Part of Lodash 4.x Collection.

Pass the required arguments shown below. Lodash Collection helpers usually return a new value — check whether the source is mutated.

Syntax

Call _.flatMap on a collection. Start with this line.

Lodash
_.flatMap(collection, [iteratee=_.identity])
CallWhat it does
_.flatMap(collection, [iteratee=_.identity])Primary signature.
_.flatMap(collection)Call with a collection argument.

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

Library declarations
Lodash
_.flatMap(collection, [iteratee=_.identity])

Parameters

ParameterDescription
collectionRequired. Array or object to iterate
iterateeOptional. Maps each value; default _.identity

Return Value

Return ValueDescription
*New array, flattened one level

Lodash Examples

Example 1 — Official: Duplicate Each Value

Return [n, n] per item; flatten into one list.

Lodash
function duplicate(n) {
  return [n, n];
}

_.flatMap([1, 2], duplicate);
// → [1, 1, 2, 2]
Try It Yourself

How It Works

  1. Iteratee. duplicate(1) → [1, 1]; duplicate(2) → [2, 2].
  2. Map shape. Intermediate would be [[1, 1], [2, 2]].
  3. Flatten. One level concatenates both pairs.
  4. Result. A single array of four numbers.

Example 2 — Split Phrases Into Words

Classic one-to-many: each string yields several tokens.

Lodash
_.flatMap(["alpha beta", "gamma"], function (chunk) {
  return chunk.split(" ");
});
// → ["alpha", "beta", "gamma"]
Try It Yourself

How It Works

  1. First phrase. "alpha beta".split(" ") → ["alpha", "beta"].
  2. Second. "gamma" → ["gamma"].
  3. Concat. Both arrays merge into one token list.
  4. vs map. _.map would leave [["alpha","beta"],["gamma"]].

Example 3 — Flatten Nested Object Fields

Pull every scopes array out of user objects into one list.

Lodash
var users = [
  { name: "Ada", scopes: ["read", "write"] },
  { name: "Lin", scopes: ["read"] }
];

_.flatMap(users, "scopes");
// → ["read", "write", "read"]
Try It Yourself

How It Works

  1. Shorthand. "scopes" is the property iteratee — reads each user’s scopes array.
  2. Ada. Contributes read and write.
  3. Lin. Contributes read.
  4. Flat list. Duplicates are kept — flatMap does not unique.

Example 4 — Non-Array Iteratee Results

Scalars are appended as single items, not wrapped in extra arrays.

Lodash
_.flatMap([1, 2, 3], function (n) {
  return n % 2 === 0 ? [n, n * 10] : n;
});
// → [1, 2, 20, 3]
Try It Yourself

How It Works

  1. Odd 1. Returns scalar 1 → one slot.
  2. Even 2. Returns [2, 20] → two slots.
  3. Odd 3. Returns scalar 3 → one slot.
  4. Mix OK. Arrays and scalars can coexist in one flatMap.

Example 5 — Only One Level of Flatten

Deeper arrays stay nested — escalate to flatMapDeep if needed.

Lodash
_.flatMap([1, 2], function (n) {
  return [[n, n]];
});
// → [[1, 1], [2, 2]]

_.flatMapDeep([1, 2], function (n) {
  return [[n, n]];
});
// → [1, 1, 2, 2]
Try It Yourself

How It Works

  1. Iteratee. Returns [[n, n]] — an array wrapping another array.
  2. flatMap. Removes only the outer wrapper → still nested pairs.
  3. flatMapDeep. Recurses until values are flat.
  4. Rule. Default depth is one — match the helper to your nesting.

Browser & Runtime Support

_.flatMap is a Lodash 4.x Collection helper. It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Native Array.prototype.flatMap is ES2019 and separate.

✓ Stable · Lodash 4.x

_.flatMap()

Available in [email protected] full builds and the lodash/flatMap modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

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
_.flatMap Lodash 4.x+

Bottom line: Safe for production once Lodash is loaded. Prefer import flatMap from "lodash/flatMap" for smaller builds. For array-only modern JS without Lodash, see native Array.flatMap.

Frequently Asked Questions

_.flatMap(collection, iteratee) maps each element through iteratee, then flattens the results one level into a new array. It is like _.map followed by _.flatten.
No. It returns a new array. The source collection is left unchanged.
Exactly one level. Deeper nesting stays nested. Use _.flatMapDeep for full recursion or _.flatMapDepth for a chosen depth.
_.map keeps one output slot per input (arrays stay nested). _.flatMap concatenates array results into one flat list.
Scalars and objects are treated as single items — they appear once in the result, not wrapped.
Native Array.prototype.flatMap (ES2019) is fine for modern array-only code. Prefer _.flatMap for object collections, Lodash iteratee shorthands, or when Lodash is already in the project.

Next: _.flatMapDeep()

Same map idea, but flatten recursively through every nested level.

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

9 people found this page helpful