-
Notifications
You must be signed in to change notification settings - Fork 0
flatMap
Subhajit Sahu edited this page Jun 18, 2020
·
14 revisions
Flattens nested lists, using map function. 🏃 📼 📦 🌔 📒
lists.flatMap(x, [fm], [ft]);
// x: nested lists
// 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.flatMap(x)];
// [ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ], [ 'de', [ [Array], [Array] ] ] ]
[...lists.flatMap(x, v => lists.flat(v, 1))];
// [
// [ 'a', 1 ],
// [ 'b', 2 ],
// [ 'c', 3 ],
// [ 'd', 4 ],
// [ 'e', [ [Array] ] ]
// ]
[...lists.flatMap(x, v => lists.flat(v))];
// [ [ 'a', 1 ], [ 'b', 2 ], [ 'c', 3 ], [ 'd', 4 ], [ 'e', 5 ] ]