Skip to content

flatMap

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

Flatten nested ientries, based on map function.

Alternatives: flat, flatMap.


function flatMap(x, fm, ft)
// x:  nested ientries
// 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.flatMap(x)];
// → [ [ "a", 1 ], [ "b", 2 ], [ "c", 3 ], [ "de", [ [Array], [Array] ] ] ]

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

[...ientries.flatMap(x, v => ientries.flat(v))];
// → [ [ "a", 1 ], [ "b", 2 ], [ "c", 3 ], [ "d", 4 ], [ "e", 5 ] ]


References

Clone this wiki locally