🧩 Maps each item and flattens the result into a single array.

Syntax

TypeScript
import { flatMap } from '@opentf/std';

flatMap<T, U>(arr: T[] = [], fn: (val: T, index: number, arr: T[]) => U[] | Iterable<U>): U[]

Parameters

  • arr: The source array.

  • fn: The mapper function returning arrays. Receives (value, index, array).

Returns

A new flattened array.

Examples

TypeScript
flatMap([1, 2], (x) => [x, x * 10]) //=> [1, 10, 2, 20]

flatMap([1, 2, 3], (x) => [x]) //=> [1, 2, 3]

flatMap(['a', 'b'], (x, i) => [x + i]) //=> ['a0', 'b1']
Last updated on
Edit this page