Skip to main content

Command Palette

Search for a command to run...

Array Flatten in JavaScript

Updated
•3 min read•View as Markdown

What nested arrays are ?

Nested arrays are simply arrays inside another array. Instead of having just one level of values, you can store groups of related data together. This is helpful when you want to organize information in a structured way, like rows and columns in a table or categories with sub-items. Each inner array can hold its own set of values, making the data more meaningful. However, accessing values can get a bit tricky because you need to use multiple indexes. Still, nested arrays are very common in programming, especially when dealing with real-world data formats like JSON.

Example : -
let array = [1, [2, 3], [4, [5, 6]]]

Why flattening arrays is useful ?

Flattening arrays is useful because it makes your data easier to work with. When arrays are deeply nested, performing tasks like searching, looping, or modifying values becomes more complicated. By flattening the array, you turn it into a simple list, which is easier to handle. This saves time and reduces the need for complex logic. It is especially helpful when you are preparing data for display or sending it somewhere, like an API. Overall, flattening improves both readability and efficiency in your code.

Example : -
[1, [2, 3], [4, 5]] -> [1, 2, 3, 4, 5]

Concept of flattening arrays

The concept of flattening arrays is about removing all levels of nesting and collecting everything into one array. You go through each element, and if it is an array, you go inside it and take out its values. This process continues until no nested arrays are left. It is often explained using recursion, where a function keeps calling itself for inner arrays. The idea is simple but very important, as it helps you deal with complex data structures in a clean way.

Example : -
let nested = [1, [2, [3, 4]]]

Different approaches to flatten arrays

There are different ways to flatten arrays depending on your approach. One common method is recursion, where the function handles each level step by step. Another way is using loops with a stack to avoid recursion. You can also use built-in methods like flat() which makes things very easy. Each method has its own use depending on how deep the array is and how efficient you want your code to be. Choosing the right method depends on the situation and problem you are solving.

Example : -
array.flat(Infinity)

Common interview scenarios

In interviews, flattening arrays is a very common problem. You may be asked to write your own function instead of using built-in methods. Sometimes, the question focuses on flattening only a certain depth or handling edge cases like empty arrays. Interviewers mainly check how well you understand recursion and logic. It is also important to write clean and readable code. Practicing these types of questions can really improve your problem-solving skills and help you feel more confident during interviews.

Example : -
function flatten(array) { return array.flat(Infinity); }