日期:2014-05-16  浏览次数:20397 次

多重数组循环
var arr=[[[[11,22],33],44],[55,66],77];
var arr2=[];
//......
console.log(arr2)
怎么让arr2的值等于11,22,33,44,55,66,77 注意arr为层数不确定的多重数组,可能是4重,5重,6重....

------解决方案--------------------
arr.toString()不行么?
------解决方案--------------------
var arr2 = arr.toString().split(',');
------解决方案--------------------
JScript code
var arr=[[[[11,22],33],44],[55,66],77];
function loop(arr)
{
    var result ="";
    for(var i = 0 ; i < arr.length ; i ++)
    {
        if(arr[i] instanceof Array) result += loop(arr[i])
        else result += arr[i].toString()+"<br/>";
    }
    return result;
}
document.write(loop(arr))

------解决方案--------------------
JScript code
        var arr = [[[[11,22],33],44],[55,66],77],
            arr2 = [];
        function cloneArray(target, source){
            for(var i = 0, len = source.length; i < len; i++){
                var el = source[i];
                if(Object.prototype.toString.call(el) === "[object Array]"){
                    cloneArray(target, el);
                }else{
                    target.push(el);
                }
            }
        }
        cloneArray(arr2, arr);
        console.log(arr2);

------解决方案--------------------
<script>
var arr=[[[[11,22],33],44],[55,66],77];
var arr2=[];
var item = arr;
var str = "";
while(item.lenght == 2) {
str = str + item[1] + ",";
item = item[0];
}
str = str + item + ","; 

arr2 = str.split(",");
alert(str);
</script>

将上面SCRIPT复制,运行, 得到结果 11,22,33,44,55,66,77,
细节方面,自己处理..
------解决方案--------------------
JScript code

<script type="text/javascript">
    var arr=[[[[11,22],33],44],[55,66],77];
    var arr2=arr.join(',').split(',');
    alert(arr2);
</script>