Getting JSON parent for node

1.6k views Asked by At

Thank you all for your feedback as this is my first question. I'll try again.

I have a JSON object as such:

var obj =     {  
       "data":[  
          {  
             "data":{  
                "title":"title1",
                "attr":{  
                   "id":111
                }
             },
             "children":[  
                {  
                   "data":{  
                      "title":"title2",
                      "attr":{  
                         "id":222
                      }
                   },
                   "children":[  
                      {  
                         "data":{  
                            "title":"title3",
                            "attr":{  
                               "id":333
                            }
                         }
                      }
                   ]
                },
                {  
                   "data":{  
                      "title":"title4",
                      "attr":{  
                         "id":444
                      }
                   },
                   "children":[  
                      {  
                         "data":{  
                            "title":"title5",
                            "attr":{  
                               "id":555
                            }
                         }
                      }
                   ]
                }
             ]
          }
       ]
    };

Given an id value, I'd like to trace a "lineage" to it through the title properties as follows: given 222, I should get title 1; given id 333, I should get title1 > title 2; given id 555, I should get title1 > title 4. Please notice these are not parents, but siblings of parents, as each data object is paired with an array of "children" objects.

I tried the following:

var x = 555;

var path = "";
function search(path, obj, target) {
    for (var k in obj) {
        if (obj[k] === target) {
            return path + "['" + k + "']"
        }
        else if (typeof obj[k] === "object") {                      
            var result = search(path + "['" + [k] + "']", obj[k], target);
            if (result)
                return result;
        }
    }
    return false;
}

var path = search(path, obj, x);
console.log(path); // "['data']['0']['children']['1']['children']['0']['data']['attr']['id']"

But I was not able to get the title attributes to return. Any help would be greatly appreciated, and thanks for everybody's patience.

1

There are 1 answers

1
Walter Chapilliquen - wZVanG On

By using loops to find the item specified can try:

var obj={"data":[{"data":{"title":"title1","attr":{"id":111}},"children":[{"data":{"title":"title2","attr":{"id":222}},"children":[{"data":{"title":"title3","attr":{"id":333}},"children":[{"data":{"title":"title4","attr":{"id":444}},"children":[{"data":{"title":"title5","attr":{"id":555}},"children":[]}]}]}]}]}]}

var current = obj.data[0], titles = [];

while(current.data && current.data.attr.id != 444){
 titles.push(current.data.title);
 current = current.children[0] || {}
}

document.write(titles.join(" > "));