缺點:會把數字轉成字符串javascript
var arr = [3, 4, 5, 6, [7, 8, [9, 10, 2, [3, 4, 5, 2]]]] arr = arr.join(',').split(',') console.log(rr)
var arr = [3, 4, 5, 6, [7, 8, [9, 10, 2, [3, 4, 5, 2]]]] let newArr = [] function flat(arr) { arr.forEach(function (item) { item instanceof Array ? flat(item) : newArr.push(item) }) } flat(arr) console.log(newArr)
function flat(arr) { let newArr = arr.reduce( (start, current) => Array.isArray(current) ? start.concat(...flat(current)) : start.concat(current), [] ) return newArr } let newArr = flat(arr) console.log(newArr)
var arr = [3, 4, 5, 6, [7, 8, [9, 10, 2, [3, 4, 5, 2]]]] const newArr = arr.flat(Infinity); console.log(newArr);