JavaScript (ES2026) reference · Array Methods
JavaScript Array flatMap() Method
Map each element with a function, then flatten the result one level. Useful when one element becomes several.
JavaScript (ES2026) Since ES2019
Syntax
array.flatMap(callbackFn, thisArg)
Parameters
| Parameter | Description |
|---|---|
callbackFn |
Called for each element with (element, index, array). |
Return value
A new array.
Examples
const sentences = ["hello world", "good morning"];
console.log(sentences.flatMap((s) => s.split(" ")));
Output
[ 'hello', 'world', 'good', 'morning' ]
const nums = [1, 2, 3, 4];
console.log(nums.flatMap((n) => (n % 2 ? [n, n] : [])));
Output
[ 1, 1, 3, 3 ]
Version history
| Version | Change |
|---|---|
| JavaScript ES2019 | Added |
Related
array.map()· Return a new array with the results of calling a function on every element. The original array is unchanged.array.flat()· Return a new array with nested arrays flattened into it, one level deep by default or to a given depth.array.filter()· Return a new array with only the elements that pass a test function. The original array is not changed.- JavaScript Arrays · tutorial
- JavaScript Array Methods · tutorial