0

Basically I have 5 foodNames variables. I want to change their values dynamically. Something like this:

        foodName1: '',
        foodAmount1: '',
        foodName2: '',
        foodAmount2: '',
        foodName3: '',
        foodAmount3: '',
        foodName4: '',
        foodAmount4: '',
        foodName5: '',
        foodAmount5: '',


addFood() {

      for (i=1; i < this.n; i++) {

       this.list.push(this.foodName{i});
       this.list.push(this.foodAmount{i});
       this.foods.push(this.list);
       this.list= [];
       this.foodName{i} = '';
       this.foodAmount{i} = '';
   }
        
}

How can I achieve that?

Dokksen
  • 372
  • 1
  • 4
  • 17

2 Answers2

0

you should put your food names in an array and access it with foodName[i]

Jan
  • 36
  • 2
0

You can access object property names dynamically using brackets:

this["foodName"+i] = "";
artoju
  • 1,612
  • 12
  • 12