flat - nodef/extra-array GitHub Wiki

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](/nodef/extra-array/wiki/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