Here is some sample code of what I am trying to achieve:
pingpong = {
paddleA: {
speed: 5,
},
paddleB: {
speed: 6,
},
paddleAI: {
AI1: "paddleA",
AI2: "paddleB"
}
}
paddleAI = pingpong.paddleAI;
paddleA = pingpong.paddleA;
paddleB = pingpong.paddleB;
for (paddle in paddleAI) {
document.write(paddleAI[paddle].speed);
}
When I run this code, it returns “undefined”
I am trying to use the text values in paddleAI as which paddle’s speed I want to access.
How would I get that code to return the speed values of paddleA and paddleB?
Note:
This is only demonstration resembling my actual code, therefore I don’t have much room to dramatically restructure how I am storing and accessing my values.
This is probably what you are after…
Using the contents of
pingpong.paddleAIto tell you what paddles are available…Your problem is that you were using
paddlein the loop expecting the paddle name, when in fact you were getting the property name ( ex…AI1)https://stackoverflow.com/a/5263872/555384
Although this seems like really bad way to do it…
Also, your trailing commas will make IE throw fits… remove the last comma in a object/array literal declaration