JavaScript Array.prototype.flatMap() Method
The flatMap() method maps each element with a function and flattens the result by one level, letting one element become several or none.
Description
The flatMap() method is the same as calling map() followed by flat() with a depth of 1, but slightly more efficient. Because the callback can return an array, each element can produce several elements, one or none in the result.
Returning an empty array removes an element, so flatMap() can filter and map in a single pass.
Only one level is flattened: if the callback returns nested arrays, the inner arrays stay nested in the result. The callback receives the same arguments as map(), the element, its index and the array. A common use is expanding records into several rows, such as turning orders into a flat list of their line items.
Syntax
array.flatMap(callbackFn)
array.flatMap(callbackFn, thisArg)Parameters
- callbackFnA function that returns a value or an array of values for each element. It receives
element,indexandarray. - thisArgOptional. A value to use as
thisinsidecallbackFn.
Return Value
A new array of the callback results, flattened by one level.
Example
Browser Support
The following information will show you the current browser support for the Array.prototype.flatMap() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





