数组扁平化
function flatArray(arr) {
if(!(arr instanceof Array)){
console.warn('目标对象非数组');
return [];
}
let result = [];
const { length } = arr;
for(let i = 0; i < length; i+=1) {
if(arr[i] instanceof Array){
result = [...result, ...flatArray(arr[i])];
}else {
result = [...result, arr[i]];
}
}
return result;
}
flatArray([1, [2, 3, [4]]]); // [1, 2, 3, 4]