For-loop to iterate over object
本问题已经有最佳答案,请猛点这里访问。
1 2 3 4 5 6 7 8 9 10 11 12 | var questions = { game: { question_1: { question:"Question 1", points: 7 }, question_2: { question:"Question 2", points: 5 } } }; |
不熟悉编程…
如何使用for循环迭代我的questions对象以访问questions.game.question_1.question?或问题。游戏。问题。问题。
1 2 3 4 5 | var answers = []; for(var i=0; i <questions.game.length; i++) { questions.game.question_[i].question.push(answers); } |
这样地:
1 2 3 4 5 | for (var key in questions.game) { if (questions.game.hasOwnProperty(key)) { questions.game[key].question.push(answers); } } |
另一方面,当您试图将数组
1 2 3 4 5 | for (var key in questions.game) { if (questions.game.hasOwnProperty(key)) { answers.push(questions.game[key].question); } } |