Skip to content
Subhajit Sahu edited this page May 3, 2023 · 20 revisions

Flatten nested array to given depth.

Alternatives: flat, flatMap.


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

var x = [[1, 2], [3, [4, [5]]]];
xarray.flat(x);
// → [ 1, 2, 3, 4, 5 ]

xarray.flat(x, 1);
// → [ 1, 2, 3, [ 4, [ 5 ] ] ]

xarray.flat(x, 2);
// → [ 1, 2, 3, 4, [ 5 ] ]


References

Clone this wiki locally