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?

3
  • 1
    possible duplicate of Using variable keys to access values in JavaScript objects Commented Jan 2, 2014 at 5: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. Commented Jan 2, 2014 at 5:08
  • Don't know if this is relative, but I'm getting this JSON object by using require: var games = require('./gameJSONobj'); Commented Jan 2, 2014 at 5:10

4 Answers 4

2

Please try

if(games.waitingGames.arithemetic[gamesize].games.length == 0)
Sign up to request clarification or add additional context in comments.

4 Comments

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".
@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.
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.
Can you paste your complete json object so that we can help you in better way ??
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.

4 Comments

like that i get a "unexpected token [" error,also tried without the dot and i get " cannot read property of 'arithemetic' of undefined =/
Sorry, typo on the ., edited. Shouldn't gametype be equal to "arithmetic"?
Also, where are you defining games? Does it work if you just write: games[gametype][gamesize].games.length
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
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){
    }
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.