1

i ve a json object as below.

[{
"data":{
    "title":"Root"
},
"attr":{
    "id":1,
    "parentId":0
},
"state":"open",
"children":[{
        "data":{
            "title":"Stem"
        },
        "attr":{
            "id":11,
            "parentId":1
        },
        "state":"open",
        "children":[{
                "data":{
                    "title":"Branch 1"
                },
                "attr":{
                    "id":111,
                    "parentId":11
                },
                "state":"open",
                "children":[{
                        "data":{
                            "title":"Sub Branch 1"
                        },
                        "attr":{
                            "id":1111,
                            "parentId":111
                        },
                        "state":"open",
                        "children":[{
                            "data":{
                                "title":"Leaf"
                            },
                            "attr":{
                                "id":111111,
                                "parentId":111
                            }
                        }]
                    },

                    {
                        "data":{
                            "title":"Sub Branch 2"
                        },
                        "attr":{
                            "id":111111,
                            "parentId":111
                        }
                    }
                ]
            },

            {
                "data":{
                        "title":"Branch 2"
                    },
                    "attr":{
                        "id":119,
                        "parentId":11
                    },
                    "state":"open",
                    "children":[{
                            "data":{
                                "title":"Sub branch"
                            },
                            "attr":{
                                "id":120,
                                "parentId":119
                            }
                        }
                    ]
            }
        ]
    }
]
}]

basically the structure would look like this

i ve the ID of all the nodes in attributes (attr). when an ID of any node is given, i want to get the json of that node and its children.

For eg. with a given ID say 111, the json for this and its children would be

{
"data":{
    "title":"Branch 1"
},
"attr":{
    "id":111,
    "parentId":11
},
"state":"open",
"children":[{
    "data":{
        "title":"Sub Branch 1"
    },
    "attr":{
        "id":1111,
        "parentId":111
    },
    "state":"open",
    "children":[{
        "data":{
            "title":"Leaf"
        },
        "attr":{
            "id":111111,
            "parentId":111
        }
    }]
    },

    {
        "data":{
            "title":"Sub Branch 2"
        },
        "attr":{
            "id":111111,
            "parentId":111
        }
    }
]
}

i want to get this json when i enter the id 111. how do i do it??

EDIT:Consider i have a GUI to enter the ID of node. and say I enter 111 i should be able to get the json as above. i just need how to do it in js or jQuery. I dont care about UI.

Thanks.!

Jonathan
  • 20,053
  • 6
  • 63
  • 70
Teja
  • 1,236
  • 12
  • 27

1 Answers1

1
function getNodeById(nodes, wantId) {
    var search = nodes.slice(0);    

    while (node = search.shift()) {
        if (node.attr.id == wantId) {
            return node;
        }

        if (node.children) {
            search.push.apply(search, node.children);
        }
    }

    return false;
}

If you pass it any level of your object, it will recursively search through the object and the children until it finds a matching ID, otherwise will return false.

To call the function:

var node = getNodeById(myArray, 1111);
console.log(node);
itsmejodie
  • 4,148
  • 1
  • 18
  • 20
  • I am getting an error as "search.shift() is not a function" in firebug. Can please give a detailed example? Thanks! – Teja Jun 06 '13 at 03:44
  • Did you pass it an array `[]` as per your example data? – itsmejodie Jun 06 '13 at 03:47
  • No! I am just passing a single node ID i.e., node.attr("id") . I want to extract all details as mentioned in the question!! To be clear, I am using jstree to reprent the json data in a tree format. – Teja Jun 06 '13 at 04:07
  • You said you have a JSON object, which was actually an array containing multiple objects. Pass the function your data, and the ID that you want! `var node = getNodeById(myData, 1111);` You will get that node object in question. – itsmejodie Jun 06 '13 at 04:12
  • @iCybernetics i saw a similar post by you. Can u help? – Teja Jun 06 '13 at 04:14
  • Sure i would give it a try and let you know.! Thanks! – Teja Jun 06 '13 at 04:15
  • http://jsfiddle.net/vvHSR/ - Provided examples based on your data – itsmejodie Jun 06 '13 at 04:16
  • in continuation to the earlier comment, To be clear, I am using jstree to reprent the json data in a tree format. So, when i click on a node, i get the ID of the clicked node (i.e., node.attr("id") ). I want to get the json of the clicked node and its children so that i can use it in the later part of my application.! Thanks! – Teja Jun 06 '13 at 04:18
  • sure. I'll try and get back to you for more help if required.! Thanks! – Teja Jun 06 '13 at 04:22
  • Thank u!! I wasnt handling the data properly. Finally I got it. – Teja Jun 10 '13 at 08:15
  • and if you could explain how exactly the code works it would be great! – Teja Jun 10 '13 at 08:16
  • And on the similar lines, i want to replace the node and its children with some other json which has same node id, but the child entities are changed. Thank u! – Teja Jun 12 '13 at 09:19