UNPKG

@extra-array/flat

Version:

Flattens nested array to given depth.

55 lines (40 loc) 1.56 kB
Flattens nested array to given depth. [:package:](https://www.npmjs.com/package/@extra-array/flat) [:smiley_cat:](https://github.com/orgs/nodef/packages?repo_name=extra-array) [:running:](https://npm.runkit.com/@extra-array/flat) [:vhs:](https://asciinema.org/a/332051) [:moon:](https://www.npmjs.com/package/@extra-array/flat.min) [:scroll:](https://unpkg.com/@extra-array/flat/) [:newspaper:](https://nodef.github.io/extra-array/) [:blue_book:](https://github.com/nodef/extra-array/wiki/) > Alternatives: [flat], [flatMap]. > This is part of package [extra-array]. [extra-array]: https://www.npmjs.com/package/extra-array <br> ```javascript array.flat(x, [n], [fm], [ft]); // x: a nested array // n: maximum depth (-1 => all) // fm: map function (v, i, x) // ft: test function (v, i, x) ``` ```javascript const array = require("extra-array"); var x = [[1, 2], [3, [4, [5]]]]; array.flat(x); // [ 1, 2, 3, 4, 5 ] array.flat(x, 1); // [ 1, 2, 3, [ 4, [ 5 ] ] ] array.flat(x, 2); // [ 1, 2, 3, 4, [ 5 ] ] ``` <br> <br> ## References - [Array.prototype.flat: MDN web docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) - [_.flatten: lodash](https://lodash.com/docs/#flatten) - [_.flattenDeep: lodash](https://lodash.com/docs/4.17.15#flattenDeep) - [_.flattenDepth: lodash](https://lodash.com/docs/4.17.15#flattenDepth) - [Array.flatten: sugarjs](https://sugarjs.com/docs/#/Array/flatten) [flat]: https://github.com/nodef/extra-array/wiki/flat [flatMap]: https://github.com/nodef/extra-array/wiki/flatMap