@lamansky/flatten

1.0.0 • Public • Published

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

  1. Bindable: arr (Array): The array to be flattened
  2. Object argument:
    • Optional: depth (integer): Defaults to Infinity. If 0, the original arr is returned.

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]

Readme

Keywords

Package Sidebar

Install

npm i @lamansky/flatten

Weekly Downloads

4,072

Version

1.0.0

License

MIT

Last publish

Collaborators

  • lamansky