0
                var roominfo = {
                'red' : {
                    'idrooms' : 1,
                    'occupants' : 0,
                    'player1' : '',
                    'player2' : '',
                    'action' : 1
                },
                'green' : {
                    'idrooms' : 2,
                    'occupants' : 0,
                    'player1' : '',
                    'player2' : '',
                    'action' : 1
                },
                'blue' : {
                    'idrooms' : 3,
                    'occupants' : 0,
                    'player1' : '',
                    'player2' : '',
                    'action' : 1
                },
                'yellow' : {
                    'idrooms' : 4,
                    'occupants' : 0,
                    'player1' : '',
                    'player2' : '',
                    'action' : 1
                },
                'purple' : {
                    'idrooms' : 5,
                    'occupants' : 0,
                    'player1' : '',
                    'player2' : '',
                    'action' : 1
                },
                'cyan' : {
                    'idrooms' : 6,
                    'occupants' : 0,
                    'player1' : '',
                    'player2' : '',
                    'action' : 1
                },
            };


            console.log("logrooms...");
            //console.log(roominfo.length);
            for (var p in roominfo) {
                    console.log(p);
                if (roominfo.hasOwnProperty(p)) {
                    // do stuff
                    console.log(p.idrooms);
                    console.log(p.occupants);
                    console.log(p.player1);
                    console.log(p.player2);
                    console.log(p.action);
                }
            }

Here is a link to my jsFiddle: http://jsfiddle.net/sepoto/66nWx/

Each room is entitled a color. So I am able to get to the roomname which in this case turns out to be the variable p. I need to access as well each nested property (idrooms,occupants,player1,player2,action). In my code these are coming up undefined right now.

How do I access the nested properties?

Thank you...

Eae
  • 4,191
  • 15
  • 55
  • 92
  • 1
    http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json – Meredith May 11 '14 at 02:11
  • Also: [How do I enumerate the properties of a javascript object?](http://stackoverflow.com/q/85992/218196). And of course before you use any statement or expression you are not very familiar with, have a look at the documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in. – Felix Kling May 11 '14 at 02:22

1 Answers1

1
roominfo[p].idrooms

Your 'p' is the key inside of roominfo. Use it like that.

geedew
  • 1,368
  • 12
  • 16