定义一个数组 arr [1, [2, [3, [4, 5]], 6]] 将该数组变成 [1,2,3,4,5,6]
let arr = [1, [2, [3, [4, 5]], 6]]
let a = arr.flat(Infinity)
console.log(a)
let a2 = JSON.stringify(arr).replace(/\[|\]/g, '').split(',')
console.log(a2)
let a3 = JSON.parse(`[${JSON.stringify(arr).replace(/\[|\]/g, '')}]`)
console.log(a3)
let a4 = []
const fn = (arr) => {
for (let i = 0; i < arr.length; i++) {
if (arr[i] instanceof Array) {
fn(arr[i])
} else {
a4.push(arr[i])
}
}
}
fn(arr)
console.log(a4)