多维数组扁平化处理
怎么把一个多维数组转化为一维数组,比如把
[1, [2, [3, 4]], [5, 6], 7]
变为
[1, 2, 3, 4, 5, 6,7]
实现方法如下:
function flattenArr(arr){ var newarr=[]; for(var i in arr){ if(typeof arr[i]==='object'){ //console.log(arr[i]); newarr=newarr.concat(arguments.callee(arr[i])); }else{ //console.log(arr[i]); newarr=newarr.concat(arr[i]); } } return newarr; } var arr = [1, [2, [3, 4]], [5, 6], 7]; var newArr = flattenArr(arr); console.log(newArr); // [1, 2, 3, 4, 5, 6,7]