Reduce way
Const arr = [1, 2, three, four, five, [6, 7, [8]]]]. function fn(arr){ return arr.reduce((prev,cur)=>{ return prev.concat(Array.isArray(cur) ? fn(cur) : cur) },[]) } fn(arr); // [1, 2, 3, 4, 5, 6, 7, 8]Copy the code
A circular manner
Const arr = [1, 2, three, four, five, [6, 7, [8]]]]. function fn(arr){ const temp = [...arr]; let resAry = []; for(let i=0; i<temp.length; i++){ const cur = temp[i]; if(Array.isArray(cur)){ temp.push(... cur) }else{ resAry.push(cur) } } return resAry; } fn(arr); // [1, 2, 3, 4, 5, 6, 7, 8] function fn1(ary) { var arr = []; const each = (data) => { if (! Array.isArray(data)) { arr.push(data); return }; data.forEach(item => { each(item); }) } each(ary) return arr; } fn1(arr); // [1, 2, 3, 4, 5, 6, 7, 8]Copy the code
The toString method
Const arr = [1, 2, three, four, five, [6, 7, [8]]]]. arr.toString(); / / '1,2,3,4,5,6,7,8'Copy the code
The join way
Const arr = [1, 2, three, four, five, [6, 7, [8]]]]. arr.join(','); / / '1,2,3,4,5,6,7,8'Copy the code