We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
扁平化就是将嵌套的数组变成一维数组。
var array = [[1,2,3],4,5,6,[[7]],[]]; flatten(array); //——> [1,2,3,4,5,6,7]
function flatten(arr) { return arr.reduce((flat, toflat)=>{ return flat.concat(Array.isArray(toflat) ? flatten(toflat) : toflat) },[]) }
function flatten(arr) { let arrStr = arr.toString().split(','); return arrStr.filter(item => item != '').map(item => parseInt(item)); }
function flatten(arr, newArr = []) { for(let item of arr){ if(Array.isArray(item)) { flatten(item, newArr); }else { newArr.push(item); } } return newArr; }
控制扁平层数
for of循环
function flat(src, n, dest = []) { if(n > 0) { for(let item of src) { if(Array.isArray(item)){ flat(item, n - 1, dest) }else { dest.push(item) } } }else { dest.push(src) } return dest }
forEach循环 + concat
function flat(src, n, dest = []) { n > 0 ? src.forEach(item => dest.concat(Array.isArray(item) ? flat(item, n - 1, dest) : dest.push(item))) : dest.push(src) return dest }
reduce + concat
function flat(src, n) { return n > 0 ? src.reduce((pre,cur) => { return pre.concat(Array.isArray(cur) ? flat(cur, n - 1) : cur) }, []) : src }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Uh oh!
There was an error while loading. Please reload this page.
扁平化就是将嵌套的数组变成一维数组。
reduce方法
toString方法
迭代方法
控制扁平层数
for of循环
控制扁平层数
forEach循环 + concat
控制扁平层数
reduce + concat
The text was updated successfully, but these errors were encountered: