I'm having trouble looping through an object using a nested $.each. The object is a series of object of the same type/class nested under rootObject.
The object
var rootObject ={};
rootObject.reportObject1.name = "reportObject1 Name";
rootObject.reportObject1.prop1 = "reportObject1 Prop1_Value";
rootObject.reportObject1.prop2 = "reportObject1 Prop2_Value";
rootObject.reportObject1.reportObjectA.name = "reportObjectA Name";
rootObject.reportObject1.reportObjectA.prop1 = "reportObjectA Prop1_Value";
rootObject.reportObject1.reportObjectA.prop2 = "reportObjectA Prop2_Value";
The Loop
$.each(rootObject, function(index0, value0){
console.log(value0.name);
$.each(value0, function(index1, value1){
console.log(value1.name);
}
}
The Problem
value1.name
is returning object properties other than name in the loop. It's seemingly attempting to return value.name forprop1
&prop2
, resulting in "undefined" values.When looking into the value of
value0
during debugging,value0
appears to loose its value as it enters the nested loop. I.e atconsole.log(value1.name)
,value0
, from the parent loop, becomesundefined
;When looking into the child loop (
index1, value1
) during debugging, I see thatvalue1
now equalsvalue0.name
, andindex1
equals "name".