https://github.com/jonkemp/flat-util
Small utility to flatten a nested array
https://github.com/jonkemp/flat-util
array flat flatten nested util utility
Last synced: about 1 year ago
JSON representation
Small utility to flatten a nested array
- Host: GitHub
- URL: https://github.com/jonkemp/flat-util
- Owner: jonkemp
- License: mit
- Created: 2020-01-22T03:53:09.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-02-01T16:43:36.000Z (over 1 year ago)
- Last Synced: 2025-04-10T00:51:06.472Z (about 1 year ago)
- Topics: array, flat, flatten, nested, util, utility
- Language: JavaScript
- Size: 312 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# flat-util 
> Flatten a nested array.
The nesting can be to any depth. If you pass shallow, the array will only be flattened one level.
## Features
- Small utility to flatten nested arrays.
- Free of TypeScript. 😄
## Install
Install with [npm](https://npmjs.org/package/flat-util)
```
$ npm install flat-util
```
Or [unpkg](https://unpkg.com/flat-util/)
```
```
Check out the unit tests on [CodePen](https://codepen.io/jonkemp/full/YzPBmwz).
## Usage
```js
const flatten = require('flat-util');
flatten([1, 2, [3, 4]]);
//=> [1, 2, 3, 4]
flatten([1, [2], [3, [[4]]]]);
//=> [1, 2, 3, 4];
flatten([1, [2], [3, [[4]]]], true);
//=> [1, 2, 3, [[4]]];
```
---
| **Like us a lot?** Help others know why you like us! **Review this package on [pkgreview.dev](https://pkgreview.dev/npm/flat-util)** | âž¡ | [](https://pkgreview.dev/npm/flat-util) |
| ----------------------------------------------------------------------------------------------------------------------------------------- | --- | --------------------------------------------------------------------------------------------------------------------- |
## API
### flatten(input, shallow)
#### input
Type: `array`
Default: `none`
The array to flatten.
#### shallow
Type: `boolean`
Default: `false`
Whether or not to flatten the array only one level.
## License
MIT