let arr = [];
function func(array) {
for (let elem of array) {
if (typeof elem == "object") {
func(elem);
} else {
arr.push(elem);
}
}
return arr;
}
console.log(func([1, [2, 7, 8], [3, 4, [5, [6, 7]]]]));
с использованием spread оператора:
function func(array) {
let arr = [];
if (typeof array == "object") {
for (let elem of array) {
arr.push(...func(elem));
}
} else {
arr.push(array);
}
return arr;
}
console.log(func([1, [2, 7, 8], [3, 4, [5, [6, 7]]]]));
с методом array.concat()
function func(array) {
let arr = [];
if (typeof array == "object") {
for (let elem of array) {
arr = arr.concat(func(elem));
}
} else {
arr.push(array);
}
return arr;
}
console.log(func([1, [2, 7, 8], [3, 4, [5, [6, 7]]]]));