Pedro Garcia
Pedro Garcia

Reputation: 13

How can i use a variable value to call an object property?

    for(i = 1; i <= 20; i++){
                
                let ingredient = meal.strIngredient + i
}

How can I put the value of variable i, in front of meal.strIngredient to pass in all strIngredient??

I need : meal.strIngredient1, meal.strIngredient2, meal.strIngredient3, meal.strIngredient4 ...

Upvotes: 1

Views: 35

Answers (1)

Evert
Evert

Reputation: 99525

You could use:

 meal['strIngredient' + i]

Upvotes: 2

Related Questions