JavaScript - Flatten a nested array



Flatten a nested array. You must account for varying levels of nesting.


Ex - steamrollArray([1, [2], [3, [[4]]]]) should return [1, 2, 3, 4].



Solution 1 Basic approach


var newArr = [];
function steamrollArray(arr) {

    for (var i = 0; i < arr.length; i++) {
        console.log(arr[i])
        if (Array.isArray(arr[i])) {
            steamrollArray(arr[i]);
        } else {
            newArr.push(arr[i]);
        }
    }
    return newArr;
}
console.log(steamrollArray([1, [2], [3, [[4]]]]));

Output:


[ 1, 2, 3, 4 ]

Solution 2 Using forEach


function steamrollArray(arr) {
    var oneArr = []
    flatten(arr);
    function flatten(arr) {
        arr.forEach(arr2 => {
            if (!Array.isArray(arr2)) {
                oneArr.push(arr2);
            }
            else {
                flatten(arr2);
            }
        });
    } return oneArr
}
console.log(steamrollArray([1, [2], [3, [[4]]]]));

Output:


[ 1, 2, 3, 4 ]

Solution 3 Ternary


function steamrollArray(arr) {
    let flat = [].concat(...arr);
    return flat.some(Array.isArray) ? steamrollArray(flat) : flat;
}

console.log(steamrollArray([1, [2], [3, [[4]]]]));

Output:


[ 1, 2, 3, 4 ]

Solution not working or have any suggestions? Please send an email to [email protected]



Join Our Facebook Group


Share this example


Download Android App

download android app

Popular javascript Examples









codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube