Skip to content
Subhajit Sahu edited this page Feb 4, 2021 · 16 revisions

Flattens nested entries to given depth. 🏃 📼 📦 🌔 📒

Alternatives: flat, flatMap.

entries.flat(x, [n], [fm], [ft]);
// x:  nested entries
// n:  maximum depth (-1 => all)
// fm: map function (v, k, x)
// ft: test function (v, k, x)
const entries = require("extra-entries");

var x = [
  ["ab", [["a", 1], ["b", 2]]],
  ["cde", [["c", 3], ["de", [["d", 4], ["e", [["e", 5]]]]]]]
];
[...entries.flat(x)];
// [ [ "a", 1 ], [ "b", 2 ], [ "c", 3 ], [ "d", 4 ], [ "e", 5 ] ]

[...entries.flat(x, 1)];
// [ [ "a", 1 ], [ "b", 2 ], [ "c", 3 ], [ "de", [ [Array], [Array] ] ] ]

[...entries.flat(x, 2)];
// [
//   [ "a", 1 ],
//   [ "b", 2 ],
//   [ "c", 3 ],
//   [ "d", 4 ],
//   [ "e", [ [Array] ] ]
// ]

references

Clone this wiki locally