Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lodash
flatten
communitycreator

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

Shubham Singh Kshatriya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The _.flatten() method in Lodash is used to flatten an array to a single level deep.

Syntax

_.flatten(array)

Parameter

This method accepts the following parameter:

  • array: This is the array to be flattened.

Return value

This method returns a single level flattened array.

Code example

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

Console
Implementation of the _.flatten() 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 _.flatten() method to flatten the array.
  • Line 9: We print the original array before flattening it.
  • Line 11: We print the output to the console.

Output

We get an array in which all the values are flattened to a single level deep.

RELATED TAGS

lodash
flatten
communitycreator

CONTRIBUTOR

Shubham Singh Kshatriya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring