Skip to content

flatMap

Subhajit Sahu edited this page Jun 18, 2020 · 14 revisions

Flattens nested lists, using map function. 🏃 [:vhs:] 📦 🌔 📒

Alternatives: flat, flatMap.

lists.flatMap(x, [fm], [ft]);
// x:  nested lists
// fm: map function (v, k, x)
// ft: test function (v, k, x)
const lists = require('extra-lists');

var x = [['ab', 'cde'], [
  [['a', 'b'], [1, 2]],
  [['c', 'de'], [
    3,
    [['d', 'e'], [
      3,
      [['e'], [
        5
      ]]
    ]]
  ]]
]];
lists.flatMap(x).map(c => [...c]);
// [ [ 'a', 'b', 'c', 'de' ], [ 1, 2, 3, [ [Array], [Array] ] ] ]

lists.flatMap(x, v => lists.flat(v, 1)).map(c => [...c]);
// [ [ 'a', 'b', 'c', 'd', 'e' ], [ 1, 2, 3, 3, [ [Array], [Array] ] ] ]

lists.flatMap(x, v => lists.flat(v)).map(c => [...c]);
// [ [ 'a', 'b', 'c', 'd', 'e' ], [ 1, 2, 3, 3, 5 ] ]

references

Clone this wiki locally