![]() |
VOOZH | about |
The _.flattenDeep() method is used to completely flatten nested arrays. It does this recursively.
Syntax:
_.flattenDeep( array )Parameters:
This method accepts single parameter as mentioned above and described below:
Return Value:
Example:
Here, const _ = require('lodash') is used to import the lodash library into the file.
Output:
[ 1, 2, 3, 4, 5 ]The _.flattenDepth() method is used to flatten up to depth time that is passed into the function.
Syntax:
_.flattenDepth(array, depth)Parameters:
This method accepts two parameters as mentioned above and described below:
Return Value:
Example 1: Flatten to depth 1
Output:
[ 1, 2, [ 3, 4, [ 5 ] ] ]Example 2: Flatten to depth 2
Output:
[ 1, 2, 3, 4, [ 5 ] ]Note: This will not work in normal JavaScript because it requires the library lodash to be installed.