定义一个数组 arr [1, [2, [3, [4, 5]], 6]] 将该数组变成 [1,2,3,4,5,6]

    1. let arr = [1, [2, [3, [4, 5]], 6]]
    2. let a = arr.flat(Infinity)
    3. console.log(a)
    4. let a2 = JSON.stringify(arr).replace(/\[|\]/g, '').split(',')
    5. console.log(a2)
    6. let a3 = JSON.parse(`[${JSON.stringify(arr).replace(/\[|\]/g, '')}]`)
    7. console.log(a3)
    8. let a4 = []
    9. const fn = (arr) => {
    10. for (let i = 0; i < arr.length; i++) {
    11. if (arr[i] instanceof Array) {
    12. fn(arr[i])
    13. } else {
    14. a4.push(arr[i])
    15. }
    16. }
    17. }
    18. fn(arr)
    19. console.log(a4)