0

I want to add items to an array using for loop but somehow i am not able to use the variable i to define the item that is being "pushed". My workaround looks like this:

if (this.antwortenNG == 1){
  this.frageObject.antwort.push(this.antwort1);
} else if(this.antwortenNG == 2){
  this.frageObject.antwort.push(this.antwort1);
  this.frageObject.antwort.push(this.antwort2);
}else if(this.antwortenNG == 3){
  this.frageObject.antwort.push(this.antwort1);
  this.frageObject.antwort.push(this.antwort2);
  this.frageObject.antwort.push(this.antwort3);
}else if(this.antwortenNG == 4){
  this.frageObject.antwort.push(this.antwort1);
  this.frageObject.antwort.push(this.antwort2);
  this.frageObject.antwort.push(this.antwort3);
  this.frageObject.antwort.push(this.antwort4);
}else if(this.antwortenNG == 5){
  this.frageObject.antwort.push(this.antwort1);
  this.frageObject.antwort.push(this.antwort2);
  this.frageObject.antwort.push(this.antwort3);
  this.frageObject.antwort.push(this.antwort4);
  this.frageObject.antwort.push(this.antwort5);
}

But i wanted to make it shorter so i tried to create a for loop but I somehow not able use the variable i to add the item. Is there anyway to solve this?

for(var i = 1; i <= this.antwortenNG; i++){
  this.frageObject.antwort.push(this.antwort+i);
}
M. Kahnke
  • 11
  • 2

0 Answers0