2

I have a JSON object that goes somewhat like this:

var waitingGames = {
    arithemetic:[
        {
            games:[]
        },

        {
            games:[]
        },

        {
            games:[]
        }
    ]

and so on for synonyms, antonyms and translation. My problem is that I can't seem to access the games array by using this:

var gametype = url_parts.query.type;
var gamesize = url_parts.query.size;
if(games.waitingGames.gametype[gamesize].games.length == 0)

I get that gametype is undefined, even though I already tried printing the variable and it has the right value (arithemetic in the above example). Any ideas how to fix this?

Monokilho
  • 71
  • 5
  • 1
    possible duplicate of [Using variable keys to access values in JavaScript objects](http://stackoverflow.com/questions/922544/using-variable-keys-to-access-values-in-javascript-objects) – nbrooks Jan 02 '14 at 05:06
  • If your key name is stored in a variable, you have to reference it using the bracket notation `object[variableName]`. It makes sense that dot-notation wouldn't work since `object.variableName` implies that there is a property of the object named `variableName`, when it should be `keyName`, which is just stored in the variable. – nbrooks Jan 02 '14 at 05:08
  • Don't know if this is relative, but I'm getting this JSON object by using require: var games = require('./gameJSONobj'); – Monokilho Jan 02 '14 at 05:10

4 Answers4

2

Please try

if(games.waitingGames.arithemetic[gamesize].games.length == 0)
  • that would kinda ruin the point because im trying to pass to the function where it should be getting then games information from. Either way, like that I also get the cannot read property of 'arithemetic' of undefined". – Monokilho Jan 02 '14 at 05:10
  • @user3152485 `waitingGames` is a variable name, not an object property, so you can't reference it by saying `games.waitingGames` -- that's why it's undefined. – nbrooks Jan 02 '14 at 05:12
  • That might be it.. any suggestion to go around it? because games has 3 variables inside it all too big to keep the main file clean. – Monokilho Jan 02 '14 at 05:17
  • Can you paste your complete json object so that we can help you in better way ?? – Durgaprasad Budhwani Jan 02 '14 at 05:45
1

Use:

games.waitingGames[gametype][gamesize].games.length

Here you are using gametype as a variable like you meant to.

See this proof-of-concept JSFiddle demo.

Robbie Wxyz
  • 7,671
  • 2
  • 32
  • 47
  • like that i get a "unexpected token [" error,also tried without the dot and i get " cannot read property of 'arithemetic' of undefined =/ – Monokilho Jan 02 '14 at 05:08
  • Sorry, typo on the `.`, edited. Shouldn't `gametype` be equal to `"arithmetic"`? – Robbie Wxyz Jan 02 '14 at 05:10
  • Also, where are you defining `games`? Does it work if you just write: `games[gametype][gamesize].games.length` – Robbie Wxyz Jan 02 '14 at 05:12
  • Yes, games is defined although from an explanation from above it might it it since i have var games = require(./gameJSONobj); and being waitingGames a variable inside that file, it won't work with the dot reference – Monokilho Jan 02 '14 at 05:16
0

You can access the value from inner games object using this expresson console.log(waitingGames.arithemetic[0].games);

Use a for loop to loop through arithemetic array.

for(var i =0, j = waitingGames.arithemetic.length; i<j; i++){
    if(waitingGames.arithemetic.games.length == 0){
    }
}
Aks
  • 1,567
  • 13
  • 23
0

Fixed, had to use the brackets to change use the variable content to reach where i wanted inside the JSON object, thanks all

Monokilho
  • 71
  • 5