We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
function flatten1(arr){ return arr.reduce((pre,cur)=>{ Array.isArray(cur)?pre = pre.concat(flatten1(cur)):pre.push(cur); return pre; },[]); }
function flatten2(arr){ let res = []; for(let i=0;i<arr.length;i++){ if(Array.isArray(arr[i])){ res = res.concat(flatten2(arr[i])); }else{ res.push(arr[i]); } } return res; }
// 原数组上修改 function flatten3(arr){ for(let i=0;i<arr.length;i++){ if(Array.isArray(arr[i])){ flatten3(arr[i]); let len = arr[i].length; arr.splice(i,1,...arr[i]); i = i+len-1; } } } let c = [1,[2,3,[4,5,6]]]; flatten3(c); console.log(c);
The text was updated successfully, but these errors were encountered:
数据扁平化并排序
function flatten(arr){ const dfs = function(arr){ return arr.reduce((pre,cur)=>{ Array.isArray(cur)?pre=pre.concat(dfs(cur)):pre.push(cur) return pre },[]) } const resultArr = dfs(arr) resultArr.sort((a,b)=>a-b) return resultArr } const arr = [[1,2,3],[4,5],9,8,7] console.log(flatten(arr))
Sorry, something went wrong.
No branches or pull requests
reduce方法
循环
在原数组上修改
The text was updated successfully, but these errors were encountered: