0

I am trying to access a particular property of an object , specifically 'levelId' in the following code.

This is what I have so far

FIDDLE

The code goes like this

$(document).ready(function(){

    var jsonList = {
        "json_data": {
            "data": [
                {
                    "data": "A node",
                    "metadata": {
                        "id": "23"
                    },
                    "children": [
                        "Child 1",
                        "A Child 2"
                    ]
                },
                {
                    "attr": {
                        "id": "li.node.id1",
                        "levelId": "3",
                        "fmnName": "Ragini"
                    },
                    "data": {
                        "title": "Long format demo",
                        "attr": {
                            "href": "#"
                        }
                    },
                    "children": [
                        {
                            "data": {
                                "title": "HQ 50 CORPS"
                            },
                            "attr": {
                                "id": 102,
                                "parentId": 101,
                                "fmnId": 194,
                                "fmnName": "PIVOT CORPS",
                                "levelId": 1,
                                "name": "HQ 50 CORPS ",
                                "isHq": "\u0000",
                                "susNum": "0415009A",
                                "modType": "Plain",
                                "seniorityId": 1,
                                "wePeNum": "HQ-38 WE 4001/1946/8 (3 DIV)",
                                "reliefId": 0,
                                "targetReliefId": 0,
                                "groupId": 0,
                                "realOrbatDtlId": 0,
                                "imgName": "10",
                                "overlayId": "0415009A",
                                "symbolCode": "1001001",
                                "locationName": "BHATINDA",
                                "nrsName": "BHATINDA"
                            },
                            "state": "open"
                        }
                    ]
                },
                {
                    "attr": {
                        "id": "li.node.id1",
                        "levelId": "3",
                        "fmnName": "Rag"
                    },
                    "data": {
                        "title": "Long format demo",
                        "attr": {
                            "href": "#"
                        }
                    }
                },
                {
                    "attr": {
                        "id": "li.node.id1",
                        "levelId": "4",
                        "name": "Skyrbe"
                    },
                    "data": {
                        "title": "Long format demo",
                        "attr": {
                            "href": "#"
                        }
                    }
                }
            ]
        }
    }
    console.log(JSON.stringify(jsonList,null,4));
    var newObject = jsonList.json_data.data;
    var listItems= "";

    $form = $("<form></form>");
    $('#form_container').append($form);

    var $selectContainer = $("<select id=\"selectId\" name=\"selectName\" />");

    for (var i = 0; i < jsonList.json_data.data.length; i++)
    {
        if(jsonList.json_data.data[i].hasOwnProperty("attr") && jsonList.json_data.data[i].attr.levelId == 3)
        {
            listItems+= "<option value='" + jsonList.json_data.data[i].attr.fmnName + "'>" + jsonList.json_data.data[i].attr.fmnName + "</option>";
        }
    }
    $($selectContainer).html(listItems);
    $($form).append($selectContainer);
});

The above code is capable of retrieving the levelId only if it is in json_data -> data -> attr. However , the levelId might be nested inside json_data -> data -> children -> attr. How do I go about iterating through this object to fetch levelId even if it's nested deep inside.

Harsha Venkataramu
  • 2,887
  • 1
  • 34
  • 58
  • you want to fetch the fname corresponding to a particular levelId or you just want all levelId (like [1,2,3,4])? –  Aug 16 '13 at 09:11

1 Answers1

0

Use a nested loop:

var data = jsonList.json_data.data;
for (var i = 0; i < data.length; i++) {
    var children = data[i].children;
    if (typeof children == 'Object') {
        for (var j = 0; j < children.length; j++) {
            var levelid = children[j].attr.levelId;
            // Do something with this child's levelid
        }
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Does this access only the levelId of children ? – Harsha Venkataramu Aug 16 '13 at 06:03
  • Yes, that's what you asked for. – Barmar Aug 16 '13 at 06:04
  • Nope , that's not what I asked for. Please re-read the question: The above code is capable of retrieving the levelId only if it is in json_data -> data -> attr. However , the levelId might be nested inside json_data -> data -> children -> attr. How do I go about iterating through this object to fetch levelId even if it's nested deep inside. – Harsha Venkataramu Aug 16 '13 at 06:06
  • And that's what this does. It iterates through the children, and gets levelId if it's in there. I just updated the code to deal with the fact that some of the `children` arrays just contain strings, not objects. – Barmar Aug 16 '13 at 06:08
  • So, if the levelId is present in json_data -> data -> attr and also in json_data -> data -> children -> attr , I should be dealing with them separately ? – Harsha Venkataramu Aug 16 '13 at 06:09
  • I see you already have the outer loop. You can merge my inner loop into that loop. – Barmar Aug 16 '13 at 06:09
  • I don't know what this data represents, or how it's intended to be used. So I don't know what it means when LevelId appears in multiple places, as it does in the data you posted. How you deal with that is your problem. – Barmar Aug 16 '13 at 06:11