Steamroller
Flatten a nested array. You must account for varying levels of nesting.
答案
freeCodeCamp Challenge Guide: Steamroller
关于展开运算符
关键点在于初始化传入第二个参数空数组用来记录
function steamrollArray(arr, res = []) {
arr.forEach(x => {
if (Array.isArray(x)) {
steamrollArray(x, res)
} else {
res.push(x)
}
})
return res
}
console.log(
steamrollArray([1, [2], [3, [[4]]]])
)