Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript flatMap() Method

JavaScript flatMap() Method

Array Object Reference JavaScript Array object

Example

Map each element using a mapping function, then compress the result into a new array:

const

arr1

=

[

1

,

2

,

[

3

]

,

[

4

,

5

]

,

6

,

[

]

]

;

const

flattened

=

arr1

.

flatMap

(

num

=>

num

)

;

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

flattened

;

Try it »


Definition and Usage

The flatMap() method first maps each element using a mapping function, then compresses the result into a new array. It is almost the same as map followed by flat with depth 1, but flatMap is usually slightly more efficient because it combines the two into one method.

flatMap() returns a new array where each element is the result of the callback function, with a structural depth of 1.


Browser support

The numbers in the table indicate the first browser version that supports this method.

Method
flatMap() 45.0 12.0 32.0 9 25.0

Syntax


// 箭头函数
flatMap((currentValue) => { /* … */ } )
flatMap((currentValue, index) => { /* … */ } )
flatMap((currentValue, index, array) => { /* … */ } )

// 回调函数
flatMap(callbackFn)
flatMap(callbackFn, thisArg)

// 行内回调函数
flatMap(function(currentValue) { /* … */ })
flatMap(function(currentValue, index) { /* … */ })
flatMap(function(currentValue, index, array){ /* … */ })
flatMap(function(currentValue, index, array) { /* … */ }, thisArg)

Parameters

Parameters Description
* callback *

A function that can generate the elements of a new array; it can take up to three arguments:

currentValue

The current element being processed in the array

indexOptional

Optional. The index of the current element being processed in the array.

arrayOptional

Optional. The array on which the callback is invoked. map arrays

| | thisArg | Optional. The value to use as "this" when executing the callback function. |

Technical Details

Return value: array.
JavaScript version: ECMAScript 6

More examples

map() vs flatMap()

Example

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]]

Although the code above seems to work with both map and flatMap, it only demonstrates how to use flatMap.

So, to better demonstrate the effect of flatMap, below we split an array containing several sentences into a new array of individual words.

Example

let arr1

=

[

"it's Sunny in"

,

""

,

"California"

]

;

arr1.

map

(

x

=>

x.

split

(

" "

)

)

;

// [["it's","Sunny","in"],[""],["California"]]

arr1.

flatMap

(

x

=>

x.

split

(

" "

)

)

;

// ["it's","Sunny","in", "", "California"]

Note that the length of the output list may differ from the length of the input list.

Add or remove items during a map()

flatMap can be used to add or remove items during a map (i.e. change the number of items). In other words, it allows you to map many items into other items (by placing them separately), rather than always one-to-one. In this sense, it works like the opposite of filter. Simply return a 1-item array to keep the item, a multi-item array to add items, or a 0-item array to remove the item.

Example

// Let's say we want to remove all the negative numbers and split the odd numbers into an even number and a 1

let a

=

[

5

,

4

,

3

,

20

,

17

,

33

,

4

,

18

]

//       |\  \  x   |  | \   x   x   |

//      [4,1, 4,   20, 16, 1,       18]

a.

flatMap

(

(

n

)

=>

 

(

n

<

0

)

?

     

[

]

:

 

(

n

%

2

==

0

)

?

[

n

]

:

                 

[

n

1

,

1

]

)

// expected output: [4, 1, 4, 20, 16, 1, 18]

Alternatives

reduce() vs concat()

Example

var

arr

=

[

1

,

2

,

3

,

4

]

;

arr.

flatMap

(

x

=>

[

x

,

x

2

]

)

;

// is equivalent to

arr.

reduce

(

(

acc

,

x

)

=>

acc.

concat

(

[

x

,

x

2

]

)

,

[

]

)

;

// [1, 2, 2, 4, 3, 6, 4, 8]

Note that this is inefficient and should be avoided for large arrays: in each iteration, it creates a new temporary array that must be garbage collected, and it copies elements from the current accumulator array into a new array instead of adding new elements to the existing array.


Array Object Reference JavaScript Array object