Array.prototype.flatMap()
flatMap(callbackFn, thisArg) Returns:
Array · Added in vES2019 · Updated March 13, 2026 · Array Methods array flatMap map flatten functional
flatMap() combines map() and flat() in a single method. It applies a function to each element, then flattens the result by one level. This is more efficient than chaining .map().flat() or .map().flat(1) because it only iterates once.
Syntax
flatMap(callbackFn, thisArg)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
callbackFn | function | — | Function that produces an element for the new array |
thisArg | any | undefined | Value to use as this when executing callbackFn |
callbackFn Parameters
| Parameter | Type | Description |
|---|---|---|
currentValue | any | The current element being processed |
index | number | The index of the current element |
array | Array | The array flatMap() was called on |
Examples
Basic usage
const words = ["hello", "world"];
const chars = words.flatMap(word => word.split(''));
console.log(chars);
// ['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']
Flattening nested arrays
const nested = [[1, 2], [3, 4], [5]];
const flattened = nested.flatMap(arr => arr.map(x => x * 2));
console.log(flattened);
// [2, 4, 6, 8, 10]
Removing empty slots
const arr = [1, , 3].flatMap(x => [x, x]);
console.log(arr);
// [1, 1, 3, 3]
Common Patterns
Flattening and transforming in one pass
Instead of:
const result = arr.map(x => [x, x * 2]).flat();
Use:
const result = arr.flatMap(x => [x, x * 2]);
Conditional flattening
const items = [1, 2, 3, 4, 5];
const result = items.flatMap(x => x % 2 === 0 ? [x] : [Global_Objects::eval]);
console.log(result);
// [2, 4]