-
Notifications
You must be signed in to change notification settings - Fork 0
flat
Subhajit Sahu edited this page Feb 4, 2021
·
14 revisions
Flattens nested lists to given depth. 🏃 📼 📦 🌔 📒
lists.flat(x, [n], [fm], [ft]);
// x: nested lists
// n: maximum depth (-1)
// fm: map function (v, k, x)
// ft: test function (v, k, x)
const lists = require('extra-lists');
var x = [['ab', 'cde'], [
[['a', 'b'], [1, 2]],
[['c', 'de'], [
3,
[['d', 'e'], [
3,
[['e'], [
5
]]
]]
]]
]];
lists.flat(x).map(c => [...c]);
// [ [ 'a', 'b', 'c', 'd', 'e' ], [ 1, 2, 3, 3, 5 ] ]
lists.flat(x, 1).map(c => [...c]);
// [ [ 'a', 'b', 'c', 'de' ], [ 1, 2, 3, [ [Array], [Array] ] ] ]
lists.flat(x, 2).map(c => [...c]);
// [ [ 'a', 'b', 'c', 'd', 'e' ], [ 1, 2, 3, 3, [ [Array], [Array] ] ] ]