0

I have a JSON object like below. I want to iterate through all the objects and it's children and form all possible nested group names: Example give below.

{
    "groups": [
        {
            "group": "group1",
            "childrens": [
                {
                    "group": "group1_1",
                    "childrens": []
                },
                {
                    "group": "group1_2",
                    "childrens": [
                        {
                            "group": "group1_2_1",
                            "childrens": []
                        },
                        {
                            "group": "group1_2_2",
                            "childrens": []
                        },
                        {
                            "group": "group1_2_3",
                            "childrens": []
                        }
                    ]
                },
                {
                    "group": "group1_3",
                    "childrens": []
                },
                {
                    "group": "group1_4",
                    "childrens": []
                }
            ]
        },
        {
            "group": "group2",
            "childrens": [
                {
                    "group": "group2_1",
                    "childrens": []
                },
                {
                    "group": "group2_2",
                    "childrens": []
                }
            ]
        }
    ]
}


Q) how to genarete the below list from above JSON
  group1
  group1/group1_1
  group1/group1_2
  group1/group1_2/group1_2_1
  group1/group1_2/group1_2_2
  group1/group1_2/group1_2_3
  group1/group1_3
  group1/group1_4
  group2
  group2/group2_1
  group2/group2_2

I have a JSON object like below. I want to iterate through all the objects and it's children and form all possible nested group names.

Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45
Dipesh Gupta
  • 138
  • 1
  • 2
  • 10
  • 2
    Great. What have you already tried. Please post your code. – Andy Aug 13 '15 at 11:32
  • it's a javascript object, no such thing as a JSON object (unless you refer the string that contains JSON as an object) - but you're not dealing with a string, you're dealing with a javascript object – Jaromanda X Aug 13 '15 at 11:34
  • possible duplicate of [Loop through nested objects with jQuery](http://stackoverflow.com/questions/17546739/loop-through-nested-objects-with-jquery) – ozil Aug 13 '15 at 12:04

1 Answers1

0

Could something like this work for you?

for(var i in OBJECT){
   iterate(OBJECT[i]);
}

function iterate(item){

    for(var i in item){
       if(typeof item[i] == "object"){
           iterate(item[i]);
       }else{
           doAction();
       }
    }

    var doAction = function(){
       ///// WHATEVER ACTION
    }

}
PRDeving
  • 679
  • 3
  • 11