1
var questions = {
game: {
    question_1: {
        question: "Question 1",
        points: 7
    },
    question_2: {
        question: "Question 2",
        points: 5
    }
}
};

New to programming...

How do I use a for-loop to iterate over my questions object to access questions.game.question_1.question? Or questions.game.question_[n].question

var answers = [];

for(var i=0; i <questions.game.length; i++) {
questions.game.question_[i].question.push(answers);
}
Nic Stelter
  • 551
  • 1
  • 6
  • 18
  • You should read up on JS objects and arrays. Objects are not arrays and do not have a length and are not accessed via indexes. –  Jan 20 '15 at 16:33

1 Answers1

1

Like this:

for (var key in questions.game) {
    if (questions.game.hasOwnProperty(key)) {
        questions.game[key].question.push(answers);
    }
}

On the other hand, you're going to run into some trouble when you try to push an array into a string. Did you have that backwards?

for (var key in questions.game) {
    if (questions.game.hasOwnProperty(key)) {
        answers.push(questions.game[key].question);
    }
}
JLRishe
  • 95,368
  • 17
  • 122
  • 158