YouTip LogoYouTip

Jsref Flatmap Array

Image 1: Array Object Reference Manual JavaScript Array Object

\n\n

Example

\n

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

\n
const arr1 = [1, 2, , [4, 5], 6, []]; const flattened = arr1.flatMap(num =>num); document.getElementById("demo").innerHTML = flattened;
\n

Try it Yourself Β»

\n\n
\n\n

Definition and Usage

\n

The flatMap() method first maps each element using a mapping function, then flattens the result into a new array. It is almost identical to calling map followed by flat with a depth of 1, but flatMap is generally slightly more efficient when merged into a single method.

\n

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

\n\n
\n\n

Browser Support

\n

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

\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Method
flatMap()45.012.032.0925.0
\n\n
\n\n

Syntax

\n
// Arrow Function flatMap((currentValue) => { /* … */ } ) flatMap((currentValue, index) => { /* … */ } ) flatMap((currentValue, index, array) => { /* … */ } )// Callback Function flatMap(callbackFn) flatMap(callbackFn, thisArg)// Inline Callback Function flatMap(function(currentValue) { /* … */ }) flatMap(function(currentValue, index) { /* … */ }) flatMap(function(currentValue, index, array){ /* … */ }) flatMap(function(currentValue, index, array) { /* … */ }, thisArg)
\n\n

Parameters

\n

Parameter | Description

\n

callback: A function that creates an element to be placed in the new array. It can take three arguments:

\n

currentValue: The current element being processed in the array.

\n

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

\n

array (Optional): The array on which map was called.

\n

thisArg (Optional): The this value to use when executing the callback function.

\n\n

Technical Details

\n

Return Value: An array.
\nJavaScript Version: ECMAScript 6

\n\n
\n\n

More Examples

\n

map() vs flatMap()

\n\n

Example

\n
var arr1 =[1,2,3,4];\n\narr1.map(x =>[x *2]);\n\n// [, , , ]\n\narr1.flatMap(x =>[x *2]);\n\n// [2, 4, 6, 8]\n\n// only one level is flattened\n\n arr1.flatMap(x =>[[x *2]]);\n\n// [, , , ]
\n

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

\n

Therefore, to better demonstrate the utility of flatMap, we will split an array containing phrases into a new array of individual words.

\n\n

Example

\n
let arr1 =["it's Sunny in","","California"];\n\narr1.map(x => x.split(" "));\n\n// [["it's","Sunny","in"],,]\n\narr1.flatMap(x => x.split(" "));\n\n// ["it's","Sunny","in", "", "California"]
\n

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

\n\n

Adding or Removing Items During a map()

\n

flatMap can be used to add or remove items during a map operation (i.e., modifying the number of items). In other words, it allows you to iterate over many items and transform them into other items (by placing them individually), rather than always maintaining a one-to-one relationship. In this sense, its function is similar to the opposite of filter. Simply return a 1-item array to keep the item, return a multi-element array to add items, or return a 0-item array to remove the item.

\n\n

Example

\n
// Let's say we want to remove all the negative numbers and split the odd numbers into an even number and a 1\n\n let a =[5,4,-3,20,17,-33,-4,18]\n\n//  |  x  | |   x  x  |\n// [4,1, 4,  20, 16, 1,  18]\n\na.flatMap((n)=>\n\n(n <0)?[]:\n\n(n %2==0)?:\n\n[n-1,1]\n\n)\n\n// expected output: [4, 1, 4, 20, 16, 1, 18]
\n\n

Alternative Approach

\n

reduce() vs concat()

\n\n

Example

\n
var arr =[1,2,3,4];\n\narr.flatMap(x =>[x, x *2]);\n\n// is equivalent to\n\n arr.reduce((acc, x)=> acc.concat([x, x *2]),[]);\n\n// [1, 2, 2, 4, 3, 6, 4, 8]
\n

Please note that this is inefficient and should be avoided with 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.

\n\n
\n\n

Image 2: Array Object Reference Manual JavaScript Array Object

← Python Func ObjectJsref At Array β†’