I can easily dynamically switch between prop2 and 3:Code:obj.prop1 obj.prop2.prop1 obj.prop3.prop1
Is there a way to do the same for obj.prop1? The following will not work, but should make clear what I intend to do:Code:var x = "prop2" return obj[x]["prop1"] var x = "prop3" return obj[x]["prop1"]
I could make an exception:Code:switch(y) { case 0: var x = ""; break; case 0: var x = "prop2"; break; case 0: var x = "prop3"; break; } //Should alert obj.prop1 if y was 0 alert(obj[x]["prop1"]);
But that becomes cumbersome when I need more than just prop1 (e.g. car.wheel, car.color, car.whatever...).Code:if(y == 0){alert(obj["prop1"]);} else { switch(y) { //And so on } }
Any way to tackle this without having to resort to eval()?


Reply With Quote
Bookmarks