//1
let arr = [1, [2, [3, [4, 5]]]];
function flatten(arr){
let res = [];
for(let i = 0; i < arr.length; i++){
if(Array.isArray(arr[i])){
res = res.concat(flatten(arr[i]))
}else{
res.push(arr[i])
}
}
return res;
}
console.log(flatten(arr))
//2
function flatten2(arr){
return arr.reduce((t, v) => {
return t.concat(Array.isArray(v) ? flatten2(v) : v);
}, [])
}
console.log(flatten2(arr));
//3
function flatten3(arr){
while(arr.some(i => Array.isArray(i))){
arr = [].concat(...arr);
}
return arr
}
console.log(flatten3(arr));
//4
function flatten4(arr){
let str = arr.toString();
return str.split(',').map(Number)
}
console.log(flatten4(arr));
//5
function faltten5(arr){
return arr.flat(Infinity);
}
console.log(faltten5(arr))
console