1

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.

rshea0
  • 11,769
  • 9
  • 27
  • 40

5 Answers5

2

This is probably what you are after...

Using the contents of pingpong.paddleAI to tell you what paddles are available...

Your problem is that you were using paddle in the loop expecting the paddle name, when in fact you were getting the property name ( ex... AI1)

https://stackoverflow.com/a/5263872/555384

for (paddle in paddleAI) {
    document.write(pingpong[paddleAI[paddle]].speed);
}

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

Community
  • 1
  • 1
jondavidjohn
  • 61,812
  • 21
  • 118
  • 158
  • I tried it and it works. I don't know why exactly it works this way, but I guess not everything has to make sense. Thanks for the answer! BTW I removed the trailing commas from my example. I don't have them in my full code, so I'm not sure why I put them there xD – rshea0 Feb 07 '12 at 18:04
  • Read up on `for..in` to understand how it works... http://stackoverflow.com/a/5263872/555384 – jondavidjohn Feb 07 '12 at 18:10
1

DEMO

You want to loop through the keys of pingpong.paddleAI, not just paddleAI. Also, you want to grab the value of the paddleAI property, not the key

for (paddle in pingpong.paddleAI) {
    var keyVal = pingpong.paddleAI[paddle]; 
    if (pingpong[keyVal].speed)
        console.log(pingpong[keyVal].speed);
    else  //if you want
        console.log("no speed on " + keyVal);
}
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
1

Think what you're after is this:

pingpong = {
    paddleA: {
        speed: 5,
    },
    paddleB: {
        speed: 6,
    },
    paddleAI: {
        AI1: "paddleA",
        AI2: "paddleB",
    },
}

for (paddle in pingpong.paddleAI) {
    document.write(pingpong[pingpong.paddleAI[paddle]].speed);
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193
1

your initial reference was off. Also, paddle would refer to the object, but you need the value (which is really a key). Code speaks louder than words: (jsFiddle demo)

var pingpong = {
    paddleA: {
        speed: 5,
    },
    paddleB: {
        speed: 6,
    },
    paddleAI: {
        AI1: "paddleA",
        AI2: "paddleB",
    },
}
//reference paddleAI
var pAI = pingpong.paddleAI;
for (paddle in pAI) {
    //here paddle reference AI1 or AI2 but we want its value
    var key = pAI[paddle];
    //and on top of that its value is a reference to the key we really want
    var paddleRef = pingpong[key];
    document.write(paddleRef.speed);
}
cannuk
  • 41
  • 2
0
alert(pingpong.paddleA.speed)
alert(pingpong.paddleb.speed)

It's a simple hierarchy, no loops required.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • As I said this is a demonstration based off my code. I know I can simply show the values, but that won't work for what I am trying to accomplish in my application. – rshea0 Feb 07 '12 at 17:46