JavaScript Array flatMap()
Example
const myArr = [1, 2, 3, 4, 5, 6];
const newArr = myArr.flatMap(x => [x, x * 10]);
Try it Yourself »
const newArr = myArr.flatMap(x => [x, x * 10]);
Description
The flatMap()
method maps all array elements
and creates a new flat array.
flatMap()
creates a new array from calling a
function for every array element.
flatMap()
does not execute the function for empty elements.
flatMap()
does not change the original array.
See Also:
Syntax
array.flatMap(function(currentValue, index, arr), thisValue)
Parameters
Parameter
Description
function()
Required.
A function to be run for each array element.
A function to be run for each array element.
currentValue
Required.
The value of the current element.
The value of the current element.
index
Optional.
The index of the current element.
The index of the current element.
arr
Optional.
The array of the current element.
The array of the current element.
thisValue
Optional.
Default value
A value passed to the function to be used as its
Default value
undefined
.A value passed to the function to be used as its
this
value.Return Value
Type
Description
An array An array with the elements as a result of a callback function and then flattened.
Array Tutorials:
Browser Support
JavaScript Array flatMap()
is supported in all modern browsers since January 2020:
Chrome 69 | Edge 79 | Firefox 62 | Safari 12 | Opera 56 |
Sep 2018 | Jan 2020 | Sep 2018 | Sep 2018 | Sep 2018 |