let a = [1,2,3,4,5,[6,7, 8, [9,[10,11,[12,[13]]]]]]
console.log(a)
// (6) [1, 2, 3, 4, 5, Array(4)]
console.log(flat(a))
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
function flat(arr) {
const temp_arr = [];
arr.forEach(x => {
if (x instanceof Array) {
temp_arr.push(...flat(x))
}else {
temp_arr.push(x)
}
})
return temp_arr;
}