多維純number陣列變成一維
let arrTest = [1, [[2], 3, 4, [5,[6]]], 7];
let arrResult = [];
function beatFlat(arr) {
for (let i=0; i<arr.length; i++) {
if (arr[i].length === undefined) {// 或者 Array.isArray(arr[i])
arrResult.push(arr[i]);
} else {
beatFlat(arr[i]);
}
}
return arrResult;
}
let beatResult = beatFlat(arrTest);
console.log("beatResult", beatResult);
複製程式碼