简体   繁体   中英

How to pass my element 1 to element 2 in a click (not find on the forum) explanation in JavaScript

I have an object like this :

this.questionnaire.Profil: {p1: {…}, p2: {…}, p3: {…}, p4: {…}, p5: {…}}

How could I display the next element after click.

My code :

for(var question in this.questionnaire.Profil["p2"]){
  this.current=this.questionnaire.Profil["p2"][question];
  console.log(question, " -> " + this.questionnaire.Profil);
  break;
}

thank you!

You can iterate your object using Object.keys

A pure JS solution :

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM