JavaScript Array flatMap() method
maps each element in an array using a mapping function and flattens the result into a new array

flatMap() method maps all array elements and creates a new flat array.

flatMap() method creates a new array from calling a function for every array element.

syntax

flatMap(callbackFn) A function to execute for each element in the array ↴

function is called with the following arguments ↴

flatMap(function(element, index, array), thisArg)

callback function allows access to each element, the index, and the original array itself.

thisArg (optional). Value to use as this when executing callbackFn. By default, it is undefined.

flatMap() method does not change the original array.

flatMap() method is identical to a map() followed by a flat() of depth 1 ↴

(myArray.map(...args).flat()); but is slightly more efficient than calling those two methods separately.

flatMap() can be used to add and remove items (modify number of items) during a map.

flatMap() method does not execute the function for empty elements because map() method does not, while flat() method ignores empty slots in the returned arrays.

syntax description
map all array elements and create a new flat array
myArray.flatMap(callbackFn)