The modern build of lodash's flattenDeep utility function that recursively flattens nested arrays to any depth.
npx @tessl/cli install tessl/npm-lodash--flattendeep@3.0.00
# lodash.flattendeep
1
2
lodash.flattendeep provides a standalone implementation of lodash's flattenDeep utility function that recursively flattens nested arrays to any depth. It offers a simple API that takes an array as input and returns a new flattened array, making it ideal for data processing scenarios where nested array structures need to be simplified.
3
4
## Package Information
5
6
- **Package Name**: lodash.flattendeep
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash.flattendeep`
10
11
## Core Imports
12
13
```javascript
14
var flattenDeep = require('lodash.flattendeep');
15
```
16
17
For ES modules:
18
19
```javascript
20
import flattenDeep from 'lodash.flattendeep';
21
```
22
23
## Basic Usage
24
25
```javascript
26
var flattenDeep = require('lodash.flattendeep');
27
28
// Flatten a deeply nested array
29
var nestedArray = [1, [2, 3, [4]]];
30
var result = flattenDeep(nestedArray);
31
console.log(result);
32
// => [1, 2, 3, 4]
33
34
// Works with any depth of nesting
35
var deeplyNested = [1, [2, [3, [4, [5]]]]];
36
var flattened = flattenDeep(deeplyNested);
37
console.log(flattened);
38
// => [1, 2, 3, 4, 5]
39
40
// Handles empty arrays and mixed types
41
var mixedArray = [1, [], [2, [3, []]], 4];
42
var mixedResult = flattenDeep(mixedArray);
43
console.log(mixedResult);
44
// => [1, 2, 3, 4]
45
```
46
47
## Capabilities
48
49
### Array Flattening
50
51
Recursively flattens a nested array to any depth, returning a new flat array with all nested elements.
52
53
```javascript { .api }
54
/**
55
* Recursively flattens a nested array.
56
*
57
* @param {Array} array The array to recursively flatten.
58
* @returns {Array} Returns the new flattened array.
59
*/
60
function flattenDeep(array);
61
```
62
63
**Parameters:**
64
- `array` (Array): The array to recursively flatten. Can be `null`, `undefined`, or an empty array.
65
66
**Returns:**
67
- (Array): Returns the new flattened array. Returns an empty array if the input is falsy or empty.
68
69
**Behavior:**
70
- Recursively processes nested arrays to any depth
71
- Preserves the order of elements as they appear when traversed depth-first
72
- Returns a new array without modifying the original
73
- Handles `null` and `undefined` inputs by returning an empty array
74
- Preserves non-array elements as-is in their flattened position
75
76
**Usage Examples:**
77
78
```javascript
79
var flattenDeep = require('lodash.flattendeep');
80
81
// Basic nested array
82
flattenDeep([1, [2, 3, [4]]]);
83
// => [1, 2, 3, 4]
84
85
// Deep nesting
86
flattenDeep([1, [2, [3, [4, [5, 6]]]]]);
87
// => [1, 2, 3, 4, 5, 6]
88
89
// Mixed types
90
flattenDeep([1, ['a', [true, [null]]]]);
91
// => [1, 'a', true, null]
92
93
// Empty arrays
94
flattenDeep([[], [1, []], [2]]);
95
// => [1, 2]
96
97
// Null/undefined input
98
flattenDeep(null);
99
// => []
100
101
flattenDeep(undefined);
102
// => []
103
104
// Empty array
105
flattenDeep([]);
106
// => []
107
```