9

有没有一种方法可以递归地遍历 JS/jQuery 对象的所有嵌套属性?

例如,给定这个对象

var x = {
    'name': 'a',
    'level': 1,
    'children': [{
        'name': 'b',
        'level': 2,
        'children': [{
            'name': 'c',
            'level': 3,
            'children': [{
            ...
            }]
        }]},
        ...
    }]
}

我怎么能循环遍历名为“a”和他们的孩子,“b”和他们的孩子,“c”和他们的孩子,无限循环的对象?

4

5 回答 5

19

递归方法似乎最好,如下所示:

function recursiveIteration(object) {
    for (var property in object) {
        if (object.hasOwnProperty(property)) {
            if (typeof object[property] == "object"){
                recursiveIteration(object[property]);
            }else{
                //found a property which is not an object, check for your conditions here
            }
        }
    }
}

这是一个工作小提琴

于 2012-09-06T08:14:34.263 回答
1

您的 JSON 格式不正确,但您可以“访问”每个项目,如下所示:

<html>
<head>
    <title>test</title>
    <script type="text/javascript">
        var x = {
                "name": "a",
                "level": 1,
                "children": [
                    {
                        "name": "b",
                        "level": 2,
                        "children": [
                            {
                                "name": "c",
                                "level": 3,
                                "children": [
                                    {
                                        "sss": 23
                                    }
                                ]
                            }
                        ]
                    }
                ]
            };

         function visit(obj){
            for(var prop in obj){
                if(typeof(obj[prop]) == 'object'){
                    if(Object.prototype.toString.call(obj[prop]) == '[object Array]'){
                        for(var i = 0; i < obj[prop].length; i++){
                            document.write("<br />the element " + prop + " (array) was visited");
                            visit(obj[prop][i]);                                
                        }
                    }else{
                        document.write("<br />the element " + prop + " (object) was visited");
                        visit(obj[prop]);
                    }
                }else{
                    document.write("<br />the element " + prop + " = " + obj[prop] + " was visited");
                }
            }
        }

        visit(x);

    </script>
</head>
<body>

</body>

于 2012-09-06T08:13:27.303 回答
1

我最初是在寻找一种递归搜索 javascript 对象的方法,我发现这篇文章非常有帮助,尤其是 Jeroen Moons 的回答。我已经对其进行了调整以提供返回对象。输入是要搜索的对象和一个{key:value}对象(针)。

function searchObject(object, keyvalue){
    var found = false;
    for (var property in object){
        if (object.hasOwnProperty(property)){
            if (typeof object[property] == 'object'){
                found = searchObject(object[property], keyvalue);
                if (found)
                    return found;
            }else{
                key = Object.keys(keyvalue)[0];
                if (property == key && object[key] == keyvalue[key]){
                    console.log('searchObject ' + keyvalue[key] + ' found');
                    return object;
                }
            }
        }
    }
}
于 2014-02-07T03:29:56.660 回答
0
var x = {
    'name': 'a',
    'level': 1,
    'children': [{
        'name': 'b',
        'level' : 2,
        'children' : [{
            'name': 'c',
            'level' : 3,
            'children' : [{
            }]
        }]
    }, {
        'name': 'b2',
        'level' : 2,
        'children' : [{
            'name': 'c2',
            'level' : 3,
            'children' : [{
            }]
        }]
    }]
}
var step = x;
do {
    if (step instanceof Array){
        for (i=0; i < step.length; i++) {
            callback(step[i]);
        }
    }
    else {
        callback(step);
    }
    step = step.children != undefined ? step.children : null;
} while (step);

function callback (element) {
    console.log(element);
}

只要结构不变,就可以这样下去。

于 2012-09-06T08:19:46.620 回答
0
function iterate(obj){
    for(var key in obj)
    {
        if(typeof(obj["key"]) == "object"){
            iterate(obj["key"]);
        }
        else{
            console.log(key + ": " + obj["key"];
        }
    }

}
于 2012-09-06T08:22:27.843 回答