Skip to content

flatMap

Subhajit Sahu edited this page Jun 15, 2020 · 19 revisions

Flattens nested iterable, based on map function. 🏃 📼 📦 🌔 📒

Alternatives: flat, flatMap.

iterable.flatMap(x, [fn]);
// x:  a nested iterable
// fn: map function (v, i, x)
const iterable = require('extra-iterable');

var x = [[1, 2], [3, [4, [5]]]];
[...iterable.flatMap(x)];
// [ 1, 2, 3, [ 4, [ 5 ] ] ]

[...iterable.flatMap(x, v => iterable.flat(v, 1))];
// [ 1, 2, 3, 4, [ 5 ] ]

[...iterable.flatMap(x, v => iterable.flat(v))];
// [ 1, 2, 3, 4, 5 ]

references

Clone this wiki locally