# Function to flatten array of multiple nested arrays without recursion in JavaScript

The problem statement asks the user that given an array of multiple nested arrays the user is asked to flatten the array and convert any more than one dimensional array into one dimensional array in javascript but without using recursion as the major condition to apply for.

Also it is one of the most asked front end javascript questions in javascript that is mostly asked for , it is given a deep nested sample array of elements all the problem statement is asking to flatten the deepest nested array into one single collection of elements in one dimension only.

## What is a Flatten Array in JavaScript?

The flatten array is the newly returned one dimension array that uses a non recursive function to concatenate sub arrays into one single array of elements in javascript. It is the process of reducing the dimensionality of the array. Also, a flattened array reduces the dimension of the original array into a lower number such that the result should solely obtain a one dimensional array only.

### Algorithm

The following problem statement dissolves with a specific algorithm that looks like :

Step 1 − Declare a function with name flatten that takes array elements as an input .

Step 2 − Declare and initialize the result array which will contain the flattened array in result of the nested array given as input . Initialize the result array with an empty array as well .

Step 3 − Declare a stack data structure to be used to solve the problem statement without recursion and initialize the stack array with all array elements given as an input by the user.

Step 4 − Declare a first Element variable to track the elements for push and pop operations with respect to stack .

Step 5 − Use the stack data structure with a while loop to check if the length of stack elements is greater than 0 to perform certain tasks.

Step 6 − Store the first element of the stack inside the first element variable .

Step 7 − Use if-else cases to check if the first element variable is a simple number of array or nested array using isArray method .

Step 8 − If the first element that is popped out of the stack is an array we use splice method in javascript to extract out the elements with in the array present and concat them with the first element stored using concat method where apply function works a binder to call one function within another function as well. This is the most important step to replace a nested array with its items reducing the dimension of array into a lower number using three javascript methods parallel.

Step 9 − If the particular first element is not an array , the number will be simply pushed into the resultant array and will be removed permanently from the original array using splice method to move forward with other elements to track and check using same while loop and if-else condition again.

Step 10 − Once the loop reaches its terminating condition , the resultant new array or you can say resultant flattened array will be returned back without using recursion.

### Example

function flatten(arr) {

var resultArray = [];

var stackOfElements = arr;

var firstElement;

while (stackOfElements.length > 0) {

firstElement = stackOfElements[0];

if (Array.isArray(firstElement)) {

Array.prototype.splice.apply(stackOfElements, [0, 1].concat(firstElement));
}

else {
resultArray.push(firstElement);

stackOfElements.splice(0, 1);

}

}

return resultArray;
}

const arr = [1, 4, 5, [
5, 6, [
6, 19, 5, [5]
], [5, 7, 6, [6, 8]], 8
], 6];

const out = flatten(arr);

console.log("output", out);


### Output

output [
1, 4, 5, 5, 6, 6,
19, 5, 5, 5, 7, 6,
6, 8, 8, 6
]


## Time and Space Complexity

The following problem statement is solved using javascript methods like splice and concat method that traverses the array of elements in O(n) worst case time complexity . The space complexity of the problem statement is O(n) because of the stack data structure used to push all the elements of the array into it .

## Conclusion

This is how we can solve the above problem statement thinking logically and in the context of coding taking help of javascript methods like split and splice method with stack data structure in its most efficient use case .

Updated on: 21-Aug-2023

342 Views