var array = [1, [2, [3, [4]], 5]];
function flat(arr) {
let numarr1 = arr.reduce(function (prev, next) {
return prev.concat(next)
}, [])
return numarr1
}
function arrFlat(arr, num) {
for (var i = 1; i <= num; i++) {
arr = flat(arr)
}
return arr
}
console.log(arrFlat(array, 1))