Open weijiyang opened 4 years ago
function flatten (arr) { while(arr.some(item => Array.isArray(item))) { arr = [].concat(...arr) } return arr }
function flatten (arr) { return arr.reduce((_arr, cur)=>{ if(Array.isArray(cur)){ _arr = _arr.concat(...flatten(cur)) } else { _arr.push(cur) } },[]) } // ES6 const flatten = array => array.reduce((acc, cur) => (Array.isArray(cur) ? [...acc, ...flatten(cur)] : [...acc, cur]), [])
迭代
递归