Skip to content
Subhajit Sahu edited this page Dec 22, 2022 · 16 revisions

Flatten nested ientries to given depth.

Alternatives: flat, flatMap.


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

var x = [
  ["ab", [["a", 1], ["b", 2]]],
  ["cde", [["c", 3], ["de", [["d", 4], ["e", [["e", 5]]]]]]]
];
[...ientries.flat(x)];
// → [ [ "a", 1 ], [ "b", 2 ], [ "c", 3 ], [ "d", 4 ], [ "e", 5 ] ]

[...ientries.flat(x, 1)];
// → [ [ "a", 1 ], [ "b", 2 ], [ "c", 3 ], [ "de", [ [Array], [Array] ] ] ]

[...ientries.flat(x, 2)];
// → [
// →   [ "a", 1 ],
// →   [ "b", 2 ],
// →   [ "c", 3 ],
// →   [ "d", 4 ],
// →   [ "e", [ [Array] ] ]
// → ]


References

Clone this wiki locally