javascript将多维数组转为一维数组
1.递归
function flat(arr,result){ if(!Object.prototype.toString.call(arr) == '[object Array]'){ return arr; } result=result||[]; for(var i=0;i<arr.length;i++){ if(Object.prototype.toString.call(arr[i]) == '[object Array]'){ result=flat(arr[i],result); }else{ result.push(arr[i]); } } return result }
2.闭包
function flat(arr){ var result=[]; function flatFn(arr1){ for(var i=0;i<arr1.length;i++){ if(!Object.prototype.toString.call(arr1[i]) == '[object Array]'){ flatFn(arr1[i]); }else{ result.push(arr1[i]); } } } flatFn(arr); return result; }