I’ve a hierarchical json object, I want to traverse it and attach a parent node to each element. This is what I’ve done
function attach_back_reference(hierarchy, parent){
hierarchy.parent = parent;
for(var i in hierarchy){
if(jQuery.isPlainObject(hierarchy[i]))
attach_back_reference(hierarchy[i], hierarchy);
}
}
But this is giving error. Maximum call stack size exceeded
Since you do
after adding the
parentproperty, one value ofiwill be"parent", so you end up setting the child as its own grandparent infinitely.You can see this in
which alerts
"x".Move the loop to the top.
Alternatively, if you only need this to work on newer interpreters, you can try making the parent property unenumerable : javascript defineProperty to make an attribute non enumerable