输入: ['a', ['b', 'c'], 3, ['d', 'e', 'f'], 2, 4]
输出:['a', 'b', 'c', 3, 'd', 'e', 'f', 2, 4]
方法一:
const arr = ['a', ['b', 'c'], 3, ['d', 'e', 'f'], 2, 4]
arr.flat(Infinity)
方法二:
let res = []
function flat(arr) {
arr.forEach(item => {
if (Array.isArray(item)) {
flat(item)
} else {
res = [...res, item]
}
})
}
方法三:
const arr = ['a', ['b', 'c'], 3, ['d', 'e', 'f'], 2, 4]
arr.toString().split(',').map(item=>+item ? +item : item)
方法四:
let arr = ['a', ['b', 'c'], 3, ['d', 'e', 'f'], 2, 4]
while (arr.some(item => Array.isArray(item))) {
arr = [].concat(...arr)
}
console.log(arr)