Open In App

JavaScript Program to Transform Nested Array into Normal Array

JavaScript allows us to convert nested arrays into a single, flat array using JavaScript. By simplifying nested structures, you can enhance the manageability and readability of your code.

Example:

Input: [[1, 2], [3, [4, 5]]]
Output: [1, 2, 3, 4, 5]

Below are the approaches to transform nested arrays into normal arrays with JavaScript:

Recursive approach

In the recursive approach we define a function flattenArray that takes a nested array as input and returns a flattened array. We iterate over each element of the input array. If an element in itself an array, we recursively call the flattenArray function on that element and concatenate the result with the result array. If an element is not an array we simply push it to the result array. This process continues until all elements of the nested array are processed.

Example: Below example uses recursive approach to transform nested array into normal array.

function flattenArray(arr) {
    let result = [];

    for (let i = 0; i < arr.length; i++) {
        if (Array.isArray(arr[i])) {
        
            // If element is an array,
            //  recursively flatten it
            result = result
                .concat(flattenArray(arr[i]));
        } else {
        
            // Otherwise add the element 
            // to the flattened array
            result
                .push(arr[i]);
        }
    }

    return result;
}

const nestedArray = [[1, 2], [3, [4, 5]]];
console.log(flattenArray(nestedArray));

Output
[ 1, 2, 3, 4, 5 ]

Time complexity: O(n), where n is the total number of elements in the nested array.

Space complexity: O(n)

Iterative approach

In the iterative approach, we use a stack data structure to flatten the nested array. We start by pushing all elements of the input array onto the stack. Then we enter a loop where we repeatedly pop an element from the stack. If the popped element is an array, we push its elements onto the stack. If the popped element is not an array, we add it to the beginning of the result array. We continue this process until the stack is empty.

Example: Below example uses iterative approach to transform nested array into normal array.

function flattenArray(arr) {
    let stack = [...arr];
    let result = [];

    while (stack.length > 0) {
        let current = stack.pop();

        if (Array.isArray(current)) {
        
            // If current element is an array, 
            // push its elements onto the stack
            stack
                .push(...current);
        } else {
        
            // Otherwise, add the element 
            // to the result array
            result
                .unshift(current);
        }
    }

    return result;
}

const nestedArray = [[4, 3], [8, 2], [9, 5]];
console.log(flattenArray(nestedArray));

Output
[ 4, 3, 8, 2, 9, 5 ]

Time complexity: O(n)

Space complexity: O(n)

Using Array.prototype.flat()

In this approach we use flat() method which is used to flatten the nested array. This method returns a new array with all the elements from the nested arrays concatenated into one array.

Example: Below example uses Array.prototype.flat() to transform nested array into normal array.

let nestedArray = [1, [2, [3, 4], 5], 6];
let flattenedArray = nestedArray.flat(Infinity);
console.log(flattenedArray);

Output
[ 1, 2, 3, 4, 5, 6 ]

Time complexity: O(n)

Space complexity: O(n)

Article Tags :