CKTIS / JavaScript
Array.prototype.flatMap: mapping and flattening one level
flatMap maps existing elements and spreads arrays returned by the callback by exactly one level.
Version: ECMAScript 2026 draft; example target: Node.js 24.22.2
Syntax
array.flatMap(callbackFn[, thisArg])array.flatMap(callbackFn[, thisArg]) calls callbackFn(value, index, array) for existing indexes and spreads each Array result by one level into a new array. A non-Array result remains one element; deeper nesting and ordinary array-like objects are not flattened again.
Source length is captured initially, holes are skipped, and the shallow result honors species. The callback can return [] to omit an item or an array of several values to expand it. A non-callable callback or null/undefined receiver throws TypeError; exceptions propagate. The method does not await a Promise returned by the callback. The example skips a hole and emits two values per visit.
Examples
const source = [1, , 2];
let visits = 0;
const result = source.flatMap(value => { visits++; return [value, value * 10]; });
console.log(result.join(","));
console.log(visits);Result
1,10,2,20 2