What is the _.flattenDeep() method in Lodash?

Overview

The _.flattenDeep() method in Lodash is used to recursively flatten an array, that is, to flatten all the nested arrays completely.

Syntax

_.flattenDeep(array)

Parameters

This method accepts the following parameter:

  • array: This is the array to be flattened.

Return value

This method returns a flattened array.

Example

Let’s look at an example of the _.flattenDeep() method in the code snippet below:

Console
Implementation of the _.flattenDeep() method

Code explanation

In the HTML tab:

  • Line 5: We import the lodash script.

In the JavaScript tab:

  • Line 2: We create an array and initialize it with a few values.
  • Line 5: We use the _.flattenDeep() method to flatten the array.
  • Line 8: We print the output to the console.

Output

We get an array in which all the values are recursively flattened.

Free Resources