function eachFlat (arr=[], depth = 1) {
const result = [];
(function flat(arr, depth) {
for (let item of arr) {
if (Array.isArray(item) && depth > 0) {
flat(item, depth - 1)
} else {
// 去除空元素,添加非undefined元素
item !== void 0 && result.push(item);
}
}
})(arr, depth)
return result
}
console.log(eachFlat([1,undefined,[2,[3,[4,5]]]],3))