JavaScript Array flatMap()
- previous page flat()
- next page forEach()
- go up one level JavaScript Array Reference Manual
Definition and usage
flatMap()
The method performs a mapping operation on each element of the array and creates a new flattened array.
flatMap()
Create a new array by calling a function for each array element.
flatMap()
Does not execute the function for empty array elements.
flatMap()
Does not change the original array.
See also:
Example
const myArr = [1, 2, 3, 4, 5, 6]; const newArr = myArr.flatMap(x => [x, x * 10]);
Syntax
array.flatMap(function(currentValue, index, arr) thisValue)
Parameters
Parameters | Description |
---|---|
function() | Required. The function to be run for each array element. |
currentValue | Required. The value of the current element. |
index | Optional. The index of the current element. |
arr | Optional. The array to which the current element belongs. |
thisValue |
Optional. The value passed to the function this. The default value is undefined. |
Return value
Type | Description |
---|---|
array | Array, containing the result elements of the callback function, and flattening it. |
Browser support
Starting from January 2020, all modern browsers support JavaScript arrays flatMap()
Method:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 69 | Edge 79 | Firefox 62 | Safari 12 | Opera 56 |
September 2018 | January 2020 | September 2018 | September 2018 | September 2018 |
- previous page flat()
- next page forEach()
- go up one level JavaScript Array Reference Manual