whoopah
whoopah

Reputation: 159

how to recursively iterate over json object/array

I am trying to figure out how to recursively iterate over this JSON no matter how many levels of nesting are present when I get this data as the amount of nesting can be any amount. I can get it to go one level deep but then it seems no matter what I do, if I want an additional level of nesting, I have to put in a test for children and although that may be correct it just doesn't seem it.

What I would like to end up with is a new data set with a 'next_id' added if the current element being looked at has a sibling or if it has a child etc.

I'm pretty confused so I hope this makes sense.

I've tried this:

    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;

}

and I call it with: visit(d) where 'd' is the data below.

Thanks in advance for any help.

So, here is my data:

    {
"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
                        }
                    }
                ]
            }
        ]
    }
]

}

Upvotes: 4

Views: 6924

Answers (1)

6502
6502

Reputation: 114461

To use recursion you need to be able to describe a problem in terms of

  1. A base case
  2. A solution computed by solving "smaller" sub-problems

For example if you want to ad an id member to each object/subobject these are

  1. If the value is not an object ignore it
  2. If it's an object then tag it and check for all sub-objects

In code

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
    }
}

this is of course an extremely simplified example and you for sure will need to add a check on what kind of objects you want to tag (for example null is considered an "object", but you cannot add attributes to it). You can also run into problems if the value passed is not a tree but a cyclic graph (i.e. if for example a.x is b and b.y is a).

Upvotes: 4

Related Questions