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