Json Back Reference
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){ hier
Solution 1:
Since you do
for(vari in hierarchy){
after adding the parent
property, one value of i
will be "parent"
, so you end up setting the child as its own grandparent infinitely.
You can see this in
var o = {};
o.x = o;
for (var i in o) { alert(i); }
which alerts "x"
.
Move the loop to the top.
functionattach_back_reference(hierarchy, parent){
for(var i in hierarchy){
if(jQuery.isPlainObject(hierarchy[i]))
attach_back_reference(hierarchy[i], hierarchy);
}
hierarchy.parent = parent;
}
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
Solution 2:
You have an infinite loop there.
You are setting the parent of each object as itself.
Post a Comment for "Json Back Reference"