We're looking for a person or people to help audit MDN to find places we could speed up. Is this you or someone you know? Check out the RFP: https://mzl.la/2IHcMiE

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The flatMap() method first maps each element using a mapping function, then flattens the result into a new array. It is identical to a map followed by a flatten of depth 1, but flatMap is quite often useful and merging both into one method is slightly more efficient.


var new_array = arr.flatMap(function callback(currentValue[, index[, array]]) {
    // return element for new_array
}[, thisArg])


Function that produces an element of the new Array, taking three arguments:
The current element being processed in the array.
The index of the current element being processed in the array.
The array map was called upon.
Value to use as this when executing callback.

Return value

A new array with each element being the result of the callback function and flattened to a depth of 1.


See Array.prototype.map() for a detailed description of the callback function. The flatMap method is identical to a map followed by a call to flatten of depth 1.


Map and flatMap

var arr1 = [1, 2, 3, 4];

arr1.map(x => [x * 2]); 
// [[2], [4], [6], [8]]

arr1.flatMap(x => [x * 2]);
// [2, 4, 6, 8]

// only one level is flattened
arr1.flatMap(x => [[x * 2]]);
// [[2], [4], [6], [8]]
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9]


Specification Status Comment
Array.prototype.flatMap proposal Draft  

Browser compatibility

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support No No No1 No No No
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support No No No No1 No No ?

1. Available in Firefox Nightly only. See bug 1435813 for status on enabling this by default.

See also

Document Tags and Contributors

 Contributors to this page: fscholz
 Last updated by: fscholz,