[英]Iterating through dictionary inside class object in JavaScript
I have a class: 我有一节课:
class Quizer {
// construct new quiz for unique user
constructor(quizObj) {
this.quiz = quizObj;
this.currentQuestionNum = 0;
this.userSelections = [];
}
...
buttonAction(setup) {
//var text = this.quiz.question1.question; <-- //works
var text = this.quiz[currentQuestionNum].question; // doesnt work
}
}
That is constructed here: 这是在这里构建的:
var quiz = new Quizer(questionObjects);
Where questionObjects is: questionObjects的位置是:
var questionObjects = {
question1: {
question: "Darwin explained his theory of evolution in a book called?",
choices: [
"this is the correct answer, choose me!",
"On the Origin of Species",
"Survival of the Fittest"
],
correctAnswer: "On the Origin of Species"
},
question2: {
...
}
}
In buttonAction, my goal is to iterate through questionObjects and get each question. 在buttonAction中,我的目标是遍历questionObjects并获取每个问题。 Can someone help me with the syntax?
有人可以帮我解释语法吗?
You need something like this 你需要这样的东西
for(var key in questionObjects){
// The following gives you the question for the current key
questionsObjects[key].question
}
As it is stated here : 正如这里所说:
The for...in statement iterates over the enumerable properties of an object, in arbitrary order.
for ... in语句以任意顺序迭代对象的可枚举属性。 For each distinct property, statements can be executed.
对于每个不同的属性,可以执行语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.