Ich habe eine Klasse:durch Wörterbuch innerhalb Klassenobjekt in JavaScript Iterieren
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
}
}
Das hier aufgebaut ist:
var quiz = new Quizer(questionObjects);
Wo questionObjects ist:
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, mein Ziel ist es, durch FrageObjects zu iterieren und jede Frage zu bekommen. Kann mir jemand bei der Syntax helfen?
Warum nicht alle Fragen in ein Array schieben? Dann können Sie iterieren –