Flattens a nested array. Pass true or 1 for one level; a larger number for deeper; omit depth (or pass false / Infinity) to flatten all the way (official Underscore docs).
Pass the required arguments shown below. Underscore array helpers usually return a new value — check whether the source array is mutated.
Syntax
Call _.flatten. Start with this line.
Underscore
_.flatten(array, [depth])
Call
What it does
_.flatten(array, [depth])
Primary signature.
_.flatten(...)
Call with the arguments your case needs.
Load Underscore with import _ from "underscore" (or the UMD CDN build) so these helpers are available as _.
Library declarations
Underscore
_.flatten(array, [depth])
Parameters
Parameter
Description
array
Required. Nested list to unwrap
depth
Optional. true/1 = one level; number = levels; omit/false/Infinity = deep
Return Value
Return Value
Description
*
New array (never mutates source)
Underscore.js Examples
Example 1 — Official: Deep Flatten
Omit depth to pull values out of every nesting level.
Deep flatten. Each inner array is unwrapped into the result.
Order. Items stay in left-to-right order.
Uniqueness? Duplicates are kept — use _.union if you need unique values.
Compatibility
Browser & Runtime Support
_.flatten ships with Underscore.js (1.x), including numeric depth. It runs anywhere Underscore runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install underscore. Native Array.prototype.flat is separate (ES2019).
✓ Stable · Underscore 1.x
_.flatten()
Available in [email protected] (UMD / ESM). Works in evergreen browsers and current Node LTS when Underscore is loaded.
100%With Underscore 1.x
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.flattenUnderscore 1.x+
Bottom line: Safe for production once Underscore is on the page or in your bundle. Not a native browser API — load Underscore first. Prefer a pinned CDN version (e.g. [email protected]).
Frequently Asked Questions
_.flatten(array, [depth]) flattens a nested array into a new array. By default it goes all the way to the deepest nesting level.
Pass true or 1 as the depth: _.flatten(arr, true) or _.flatten(arr, 1). Deeper nests stay as arrays.
Yes. Pass a number (for example 2) to descend that many levels. Omit depth, or pass false or Infinity, for a full deep flatten.
No. It returns a new array. The source nesting is left unchanged. Object elements are still shared by reference.
_.flatten only unwraps nested arrays. _.union merges arrays and also keeps unique values in first-seen order.
Array.prototype.flat is ES2019 and uses a similar depth idea (default 1). Underscore defaults to deep flatten, and true means one level — opposite default depth from native flat().