method Enumerable.prototype.flatMap
Enumerable.prototype.flatMap<U>(mapFn: (item: T) => U | Promise<U>): Enumerable<ElementType<U>>

Map each item to an iterable and flatten the results.

Equivalent to calling map() followed by flatten().

Examples

Duplicate and scale each number

import { enumerate } from "jsr:@j50n/proc";

const result = await enumerate([1, 2, 3])
  .flatMap(n => [n, n * 10])
  .collect();
// [1, 10, 2, 20, 3, 30]

Type Parameters

Parameters

mapFn: (item: T) => U | Promise<U>

The mapping function.

Return Type

Enumerable<ElementType<U>>

An Enumerable of flattened mapped values.

Usage

import { Enumerable } from ".";