JavaScript Array flatMap()

definition and usage

flatMap() the method performs mapping operations 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:

Array map() method

Array flat() method

Array filter() method

Array forEach() method

example

const myArr = [1, 2, 3, 4, 5, 6];
const newArr = myArr.flatMap(x => [x, x * 10]);

try it yourself

syntax

array.flatMap(function(currentValue, index, arr) thisValue)

parameter

parameter 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, including the result elements of the callback function, and flatten it.

browser support

Starting from January 2020, all modern browsers support JavaScript array 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