Skip to content

flatMap

Subhajit Sahu edited this page Jun 11, 2020 · 13 revisions

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

Alternatives: flat, flatMap.

object.flatMap(x, [fn], [ths]);
// x:   a nested object
// fn:  map function (v, k, x)
// ths: this argument
const object = require('extra-object');

var x = {ab: {a: 1, b: 2}, cde: {c: 3, de: {d: 4, e: {e: 5}}}};
object.flatMap(x);
// { a: 1, b: 2, c: 3, de: { d: 4, e: { e: 5 } } }

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

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

references

Clone this wiki locally