Skip to content

flatMap

Subhajit Sahu edited this page Jun 17, 2020 · 16 revisions

Flattens nested entries, using map function. 🏃 📼 📦 🌔 📒

Alternatives: [flat], [flatMap].

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

var x = new Map([
  ['ab', new Map([
    ['a', 1],
    ['b', 2]
  ])],
  ['cde', new Map([
    ['c', 3],
    ['de', new Map([
      ['d', 4],
      ['e', new Map([
        ['e', 5]
      ])]
    ])]
  ])]
]);
entries.flatMap(x);
// Map(4) {
//   'a' => 1,
//   'b' => 2,
//   'c' => 3,
//   'de' => Map(2) { 'd' => 4, 'e' => Map(1) { 'e' => 5 } }
// }

entries.flatMap(x, v => entries.flat(v, 1));
// Map(5) {
//   'a' => 1,
//   'b' => 2,
//   'c' => 3,
//   'd' => 4,
//   'e' => Map(1) { 'e' => 5 }
// }

entries.flatMap(x, v => entries.flat(v));
// Map(5) { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5 }

references

Clone this wiki locally