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?

4

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

You can access object property names dynamically using brackets:

this["foodName"+i] = "";

3 Comments

it says 'i' is not defined
It says 'foodName' isn't defined on the instance.
Then you need to provide more code to see where the problem lies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.