简体   繁体   中英

Javascript nested array to integer

I have a nested array

const folder = [1, [2, [3, [4, [5, [6]]]]]];

please use the basic concept of javaScript. I need return like 123456. what should I do? i tried using for loops but it didn't work.

use the recursive function here. find the below solution

const folder = [1, [2, [3, [4, [5, [6]]]]]];

let result = '';

function convert(arr) {
    arr.forEach((val) => {
        if (Array.isArray(val)) {
            convert(val);
        } else {
            result = result + val
        }
    })

    return result
}

console.log(convert(folder))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM