Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/iter/mod.ts>flatMap

A bunch of utilities for working with iterables, many inspired by the native array methods.
Latest
function flatMap
import { flatMap } from "https://deno.land/x/iter@v3.2.3/mod.ts";

Lazily calls a defined callback function for each element of an iterable, and returns a new flattened iterable of the results by one level.

Examples

Example 1

import * as iter from "https://deno.land/x/iter/mod.ts";

const numbers = iter.create.increments(1);
const squares = iter.flatMap(numbers, n => [n, n*n]);
const iterator = squares[Symbol.iterator]();

console.log(iterator.next().value); // -> 1
console.log(iterator.next().value); // -> 1
console.log(iterator.next().value); // -> 2
console.log(iterator.next().value); // -> 4
console.log(iterator.next().value); // -> 3
console.log(iterator.next().value); // -> 9

Type Parameters

T
optional
U = T

Parameters

it: Iterable<T>
  • The iterable being mapped.
  • A function that accepts up to three arguments. The flatMap function calls f function one time for each item in the iterable.

Returns

Iterable<U>

An iterable of f applied to items of it.