@lamansky/flatten
v1.0.0
Published
A one-liner function that flattens arrays.
Downloads
8,045
Readme
flatten
A one-liner function that flattens arrays.
Installation
Requires Node.js 6.0.0 or above.
npm i @lamansky/flatten
API
The module exports a single function.
Parameters
- Bindable:
arr
(Array): The array to be flattened - Object argument:
- Optional:
depth
(integer): Defaults toInfinity
. If0
, the originalarr
is returned.
- Optional:
Return Value
The flattened Array
Example
const flatten = require('@lamansky/flatten')
const arr = [[[1], 2], [3]]
flatten(arr) // [1, 2, 3]
flatten(arr, {depth: 1}) // [[1], 2, 3]
flatten(arr, {depth: 0}) // [[[1], 2], [3]]
// Supports the bind operator
arr::flatten() // [1, 2, 3]
arr::flatten({depth: 1}) // [[1], 2, 3]