Standard built-ins
Array.prototype.flatMap()
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
| Name | Type | Description |
|---|---|---|
| callback | function | Returns a scalar or array for each present source index. |
| thisArg | any | Optional 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(","));