0

I have a JSON object that has a nested structure like this: http://pastebin.com/tNXNDVQe . I am trying to find all the elements that have a common parent, in the below example the parent is PRI. It is not clear to me why I can not get the parent name like this: pdData.parent.name ?

I am recursively checking each and every object like this:

var pdName = ["Damage Ratio"];
var pdParent = ["PRI"];
var pdData = // the JSON object


        function findTreeBranchInfo(pdData, pdName, pdParent) {
            if (pdParent.some(function(currentValue) {
                console.log(pdData);
                return (pdData.parent.name == currentValue); //does not work

            })) {
                console.log("match");
                pdTempIds.push(pdData.id);
                createSpinner(pdData.id, pdData.weight, pdData.name);
            }

            (pdData.children || []).forEach(function(currentItem) {
                findTreeBranchInfo(currentItem, [pdName], [pdParent]);
            });
        }

In the above loop, console.log(pdData); returns:

Object {name: "Damage Ratio", weight: 33, level: 3.1, depth: 2, parent: Object…}
    .   depth: 2
    .   id: 40
    .   level: 3.1
    .   name: "Damage Ratio"
    .   parent: Object
      .   children: Array[3]
      .   depth: 1
      .   id: 41
      .   level: 2
      .   name: "PRI"
      .   parent: Object
      .   weight: 50
      .   x: 40
      .   x0: 40
      .   y: 180
      .   y0: 180
      .   __proto__: Object 
Bwyss
  • 1,736
  • 3
  • 25
  • 48
  • Could you create a fiddle? I'd like to inspect that Object – Jonas Grumann Mar 12 '14 at 09:19
  • Already answered at http://stackoverflow.com/questions/2980763/javascript-objects-get-parent. I am sorry to say you will be disappointed. – Tejas Kale Mar 12 '14 at 09:25
  • *I am trying to find all the elements that have a common parent* you want to get all the childrens of a given object, right ? – Serge K. Mar 12 '14 at 09:47
  • Nathan P, yes that is correct – Bwyss Mar 12 '14 at 10:32
  • inside a D3.js function I am able to access the parents name like this: `nodeEnter.append("text") .on("click", function(d) { pdName = d.name; pdData = data; pdWeight = d.weight; pdParent = d.parent.name; $('#projectDefWeightDialog').empty(); findTreeBranchInfo(pdData, [pdName], [pdParent]); updateButton(); });` – Bwyss Mar 12 '14 at 10:37

0 Answers0