Skip to content
Subhajit Sahu edited this page Dec 8, 2022 · 13 revisions

Flatten nested object to given depth.

Similar: flat, flatMap.


function flat(x, n, fm, ft)
// x:  a nested object
// n:  maximum depth [-1 ⇒ all]
// fm: map function (v, k, x)
// ft: test function for flatten (v, k, x)
const object = require('extra-object');

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

object.flat(x, 1);
// → { a: 1, b: 2, c: 3, de: { d: 4, e: { e: 5 } } }

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


References

Clone this wiki locally