【问题标题】:how to recursively iterate over json object/array如何递归迭代json对象/数组
【发布时间】:2016-04-15 22:17:38
【问题描述】:

我试图弄清楚如何递归地迭代这个 JSON,不管当我得到这个数据时存在多少嵌套级别,因为嵌套的数量可以是任意数量。我可以让它更深一层但似乎无论我做什么,如果我想要额外的嵌套级别,我必须对孩子进行测试,虽然这可能是正确的,但它似乎并不.

如果当前正在查看的元素有兄弟姐妹或有子元素等,我想最终得到一个新数据集,并添加一个“next_id”。

我很困惑,所以我希望这是有道理的。

我试过了:

    function visit(obj){
var msg = '';
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++){
                msg += "the element " + prop + " (array) was visited: " + obj[prop][i].key + "\n";
                if (  obj[prop][i].children )
                {
                    msg += "the element " + prop + " (array-CHILDREN) was visited: " + obj[prop][i].key + "\n";
                    for ( y=0;y<obj[prop][i].children.length;y++ )
                    {
                        msg += "the element " + prop + " (array-HELLO) was visited: " + obj[prop][i].children[y].title + "\n";
                        if (  obj[prop][i].children[y].children )
                        {
                            msg += "the element " + prop + " (array-HELLO-CHILD) was visited: " + obj[prop][i].children[y].title + "\n";
                            $.each(obj[prop][i].children[y].children, function( index, value ) {
                                alert( "red alert " + value.title );
                                msg += "the element " + prop + " (CHILD-CHILD) was visited: " + i + ": " + index + ": " + value.title + "\n";
                                visit(value);  
                            });
                        }

                    }
                }
            }
        }else{
            msg += "the element " + prop + " (object) was visited"  + "hello" + "\n";
            visit(obj[prop]);
        }
    }else{
        msg += "the element " + prop + " = " + obj[prop] + " was visited"  + "\n";
    }
}
return msg;

}

我称之为: visit(d) 其中“d”是以下数据。

提前感谢您的帮助。

所以,这是我的数据:

    {
"key": "root_1",
"title": "root",
"children": [
    {
        "key": "1",
        "title": "Title Goes Here",
        "data": {
            "can_drag": false,
            "can_drop": false
        }
    },
    {
        "folder": false,
        "key": "_1",
        "title": "Text for regular step goes here",
        "data": {
            "step_type": "span",
            "imagePath": "images/",
            "icon": "text_align_center.png",
            "can_drag": true,
            "can_drop": [
                "before",
                "after"
            ]
        }
    },
    {
        "folder": false,
        "key": "_2",
        "title": "Text for regular step goes here",
        "data": {
            "step_type": "span",
            "imagePath": "images/",
            "icon": "text_align_center.png",
            "can_drag": true,
            "can_drop": [
                "before",
                "after"
            ]
        }
    },
    {
        "expanded": true,
        "folder": true,
        "key": "_3",
        "title": "Fork question here",
        "data": {
            "step_type": "fork",
            "imagePath": "images/",
            "icon": "fork.gif",
            "can_drag": true,
            "can_drop": false
        },
        "children": [
            {
                "expanded": true,
                "key": "_4",
                "title": "branch 1 answer text",
                "data": {
                    "step_type": "fork_branch",
                    "imagePath": "images/",
                    "icon": "arrow_right.png",
                    "can_drag": true,
                    "can_drop": false
                },
                "children": [
                    {
                        "key": "_5",
                        "title": "branch 1 answer text first step",
                        "data": {
                            "imagePath": "images/",
                            "icon": "text_align_center.png",
                            "step_type": "step",
                            "can_drag": true,
                            "can_drop": false
                        }
                    },
                    {
                        "expanded": true,
                        "folder": true,
                        "key": "_8",
                        "title": "Fork question here",
                        "data": {
                            "step_type": "fork",
                            "imagePath": "images/",
                            "icon": "fork.gif",
                            "can_drag": true,
                            "can_drop": false
                        },
                        "children": [
                            {
                                "expanded": true,
                                "key": "_9",
                                "title": "branch 1 answer text",
                                "data": {
                                    "step_type": "fork_branch",
                                    "imagePath": "images/",
                                    "icon": "arrow_right.png",
                                    "can_drag": true,
                                    "can_drop": false
                                },
                                "children": [
                                    {
                                        "key": "_10",
                                        "title": "branch 1 answer text first step",
                                        "data": {
                                            "imagePath": "images/",
                                            "icon": "text_align_center.png",
                                            "step_type": "step",
                                            "can_drag": true,
                                            "can_drop": false
                                        }
                                    },
                                    {
                                        "expanded": true,
                                        "folder": true,
                                        "key": "_13",
                                        "title": "Fork question here",
                                        "data": {
                                            "step_type": "fork",
                                            "imagePath": "images/",
                                            "icon": "fork.gif",
                                            "can_drag": true,
                                            "can_drop": false
                                        },
                                        "children": [
                                            {
                                                "expanded": true,
                                                "key": "_14",
                                                "title": "branch 1 answer text",
                                                "data": {
                                                    "step_type": "fork_branch",
                                                    "imagePath": "images/",
                                                    "icon": "arrow_right.png",
                                                    "can_drag": true,
                                                    "can_drop": false
                                                },
                                                "children": [
                                                    {
                                                        "key": "_15",
                                                        "title": "branch 1 answer text first step",
                                                        "data": {
                                                            "imagePath": "images/",
                                                            "icon": "text_align_center.png",
                                                            "step_type": "step",
                                                            "can_drag": true,
                                                            "can_drop": false
                                                        }
                                                    },
                                                    {
                                                        "folder": false,
                                                        "key": "_19",
                                                        "title": "End script text goes here",
                                                        "data": {
                                                            "step_type": "end",
                                                            "imagePath": "images/",
                                                            "icon": "text_align_center.png",
                                                            "can_drag": true,
                                                            "can_drop": false
                                                        }
                                                    }
                                                ]
                                            },
                                            {
                                                "expanded": true,
                                                "key": "_16",
                                                "title": "branch 2 answer text",
                                                "data": {
                                                    "step_type": "fork_branch",
                                                    "imagePath": "images/",
                                                    "icon": "arrow_right.png",
                                                    "can_drag": true,
                                                    "can_drop": false
                                                },
                                                "children": [
                                                    {
                                                        "key": "_17",
                                                        "title": "branch 2 answer text first step",
                                                        "data": {
                                                            "imagePath": "images/",
                                                            "icon": "text_align_center.png",
                                                            "step_type": "step",
                                                            "can_drag": true,
                                                            "can_drop": false
                                                        }
                                                    },
                                                    {
                                                        "folder": false,
                                                        "key": "_20",
                                                        "title": "Text for regular step goes here",
                                                        "data": {
                                                            "step_type": "span",
                                                            "imagePath": "images/",
                                                            "icon": "text_align_center.png",
                                                            "can_drag": true,
                                                            "can_drop": [
                                                                "before",
                                                                "after"
                                                            ]
                                                        }
                                                    },
                                                    {
                                                        "folder": false,
                                                        "key": "_21",
                                                        "title": "End script text goes here",
                                                        "data": {
                                                            "step_type": "end",
                                                            "imagePath": "images/",
                                                            "icon": "text_align_center.png",
                                                            "can_drag": true,
                                                            "can_drop": false
                                                        }
                                                    }
                                                ]
                                            }
                                        ]
                                    }
                                ]
                            },
                            {
                                "expanded": true,
                                "key": "_11",
                                "title": "branch 2 answer text",
                                "data": {
                                    "step_type": "fork_branch",
                                    "imagePath": "images/",
                                    "icon": "arrow_right.png",
                                    "can_drag": true,
                                    "can_drop": false
                                },
                                "children": [
                                    {
                                        "key": "_12",
                                        "title": "branch 2 answer text first step",
                                        "data": {
                                            "imagePath": "images/",
                                            "icon": "text_align_center.png",
                                            "step_type": "step",
                                            "can_drag": true,
                                            "can_drop": false
                                        }
                                    },
                                    {
                                        "folder": false,
                                        "key": "_22",
                                        "title": "End script text goes here",
                                        "data": {
                                            "step_type": "end",
                                            "imagePath": "images/",
                                            "icon": "text_align_center.png",
                                            "can_drag": true,
                                            "can_drop": false
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                "expanded": true,
                "key": "_6",
                "title": "branch 2 answer text",
                "data": {
                    "step_type": "fork_branch",
                    "imagePath": "images/",
                    "icon": "arrow_right.png",
                    "can_drag": true,
                    "can_drop": false
                },
                "children": [
                    {
                        "key": "_7",
                        "title": "branch 2 answer text first step",
                        "data": {
                            "imagePath": "images/",
                            "icon": "text_align_center.png",
                            "step_type": "step",
                            "can_drag": true,
                            "can_drop": false
                        }
                    },
                    {
                        "folder": false,
                        "key": "_18",
                        "title": "End script text goes here",
                        "data": {
                            "step_type": "end",
                            "imagePath": "images/",
                            "icon": "text_align_center.png",
                            "can_drag": true,
                            "can_drop": false
                        }
                    }
                ]
            }
        ]
    }
]

}

【问题讨论】:

  • 仅供参考,JSON 是一种文本格式。您正在迭代的是一个 javascript 对象。
  • 你使用递归。如果属性类型是对象,则递归调用相同的函数来迭代它的所有属性。如果它是一个数组,则循环遍历该数组并在每个数组元素上递归调用该函数。如果它不是数组或对象,你只需处理它的值。

标签: jquery recursion


【解决方案1】:

要使用递归,您需要能够用以下方式描述问题

  1. 基本案例
  2. 通过解决“较小”子问题计算出的解决方案

例如,如果您想为每个对象/子对象添加一个 id 成员,这些是

  1. 如果值不是对象,则忽略它
  2. 如果它是一个对象,则标记它并检查所有子对象

在代码中

var next_id = 0;
function tag_value(x) {
    if (typeof x == "object") {
        x.id = ++next_id; // Add the tag
        for (var attr in x) {
            // Recursive call to solve the "smaller" problem
            tag_value(x[attr]);
        }
    } else {
        // Not an object, ignore
    }
}

这当然是一个非常简单的例子,你肯定需要检查你想要标记的对象类型(例如null被认为是一个“对象”,但你不能给它添加属性)。如果传递的值不是树而是循环图,您也可能会遇到问题(例如,如果 a.xb 并且 b.ya)。

【讨论】:

    猜你喜欢
    • 2015-06-04
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 2016-11-13
    • 2019-08-11
    • 2020-01-05
    相关资源
    最近更新 更多