我有一个这样的对象:
this.questionnaire.Profil: {p1: {…}, p2: {…}, p3: {…}, p4: {…}, p5: {…}}
如何在点击后显示下一个元素。
我的代码:
for(var question in this.questionnaire.Profil["p2"]){
this.current=this.questionnaire.Profil["p2"][question];
console.log(question, " -> " + this.questionnaire.Profil);
break;
}
谢谢你!
答案 0 :(得分:0)
您可以使用Object.keys
迭代您的对象纯JS解决方案:
var questions = {p1: {question:"question1"}, p2: {question:"question2"}, p3: {question:"question3"}, p4: {question:"question4"}, p5: {question:"question5"}};
var current = Object.keys(questions)[0];
function getNextQuestion(){
console.log(questions[current].question);
var keys = Object.keys(questions);
var index = keys.indexOf(current);
if(keys[index + 1] && questions[keys[index + 1]]){ //there is a next element
current = keys[index + 1];
} else {
current = keys[0];
}
}

<button class="btn-next" ion-button required onclick="getNextQuestion()" round block>Next</button>
&#13;