繁体   English   中英

在JavaScript中循环遍历数组数组

[英]Loop through array of arrays in javascript

我有一个看起来像这样的数组:

var arrayElements=
[
        ["1","description 1",
            [
                ["1.1","description 1.1"],
                ["1.2","description 1.2"],
                ["1.2","description 1.3"]
            ]
        ]
        ,
        ["2","description 2"],
        ["3","description 3",
            [
                ["3.1","description 3.1"],
                ["3.2","description 3.2",
                    [
                        ["3.2.1","description 3.2.1"],
                        ["3.2.2","description 3.2.2"],
                        ["3.2.2","description 3.2.3"]
                    ]
                ],
                ["3.3","description 3.3"]
            ]
        ],
        ["4","description 4"]
];

我想循环通过以下数组,能够访问所有值以及每个元素的打印深度。 控制台中的预期结果将是这样的:

description 1, depth 1
description 1.1, depth 2
description 1.2, depth 2
description 1.3, depth 2
description 2, depth 1
description 3, depth 1
description 3.1, depth 2
description 3.2, depth 2
description 3.2.1, depth 3
description 3.2.2, depth 3
description 3.2.3, depth 3
description 3.3, depth 2
description 4, depth 1

这是我尝试过的:

var depth = 0;

function listNodes(array){

    for(i = 0; i < array.length;i++){
        if( /* itearating over root array */ ){
            depth = 1;
        }

        if(array[i][2] instanceof Array){
            depth++;
            console.log(array[i][1] + " depth: "+depth);
            listNodes(array[i][2]);
        }else{
            console.log(array[i][1] + " depth: "+depth);
        }
    }
}

listNodes(arrayElements);

我无法遍历此数组,即使它应该继续,它也会卡住。 另一个问题是我的变量存储深度无法正常工作,因为我无法确定何时在根数组上进行循环迭代,从而无法重置depth计数器。 有任何想法吗?

 var arrayElements= [ ["1","description 1", [ ["1.1","description 1.1"], ["1.2","description 1.2"], ["1.2","description 1.3"] ] ] , ["2","description 2"], ["3","description 3", [ ["3.1","description 3.1"], ["3.2","description 3.2", [ ["3.2.1","description 3.2.1"], ["3.2.2","description 3.2.2"], ["3.2.2","description 3.2.3"] ] ], ["3.3","description 3.3"] ] ], ["4","description 4"] ]; function listNodes(array, depth) { depth = typeof depth === 'undefined' ? 1 : depth; var len = array.length; for(var i = 0; i < array.length;i++) { console.log(array[i][1] + " depth: "+ depth); if(array[i][2] instanceof Array) { listNodes(array[i][2], depth + 1); } } } listNodes(arrayElements); 

这给了我您示例的确切输出。 请注意,我将深度作为参数传递给函数。

如果您可以使用对象而不是记住索引,那么它将更漂亮,更灵活:

 var arrayElements = [{ id: "1", description: "description 1", children: [{ id: "1.1", description: "description 1.1", children: [] }] }, { id: "2", description: "description 2", children: [{ id: "2.1", description: "description 2.1", children: [{ id: "2.1.1", description: "description 2.1.1", children: [] }, { id: "2.1.2", description: "description 2.1.2", children: [] } ] }] } ]; function deepSearch(arr, depth, cb) { if (depth === void 0) { depth = 0; } if (cb === void 0) { cb = function(val) { console.log(val); }; } for (var i = 0; i < arr.length; i++) { var val = arr[i]; cb(val.id + " - " + val.description + " at depth " + depth); if (val.children.length > 0) { deepSearch(val.children, depth + 1, cb); } } } deepSearch(arrayElements); 

你可以做这样的事情

function listNodes(array, depth) {
    depth++;
    for (let i = 0; i < array.length; i++) {
        console.log(array[i][1] + ' - ' + depth);
        if (typeof array[i][2] === 'object') {
            listNodes(array[i][2], depth);
        }
    }
}

listNodes(arrayElements, 0);

考虑到您的示例,这将是:

description 1 - 1
description 1.1 - 2
description 1.2 - 2
description 1.3 - 2
description 2 - 1
description 3 - 1
description 3.1 - 2
description 3.2 - 2
description 3.2.1 - 3
description 3.2.2 - 3
description 3.2.3 - 3
description 3.3 - 2
description 4 - 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM