Standard built-ins

Array.prototype.flatMap()

Try the free JavaScript compiler

Maps each present source index and flattens array results one level. Empty returned arrays contribute nothing; nonarray results contribute one value. Ordinary source holes are skipped.

Formal Syntax & Structure

javascript syntaxjavascript
array.flatMap(callback, thisArg?)

Parameters & Attributes

NameTypeDescription
callbackfunctionReturns a scalar or array for each present source index.
thisArganyOptional this value for ordinary callback functions.

Return Value

Array

Mapped and flattened result.

Working Example

Example Snippetjavascript
console.log([1,0,2].flatMap(x=>x?[x,x]:[]).join(","));
JavaScript Reference