0

The API I'm calling has a section of Ingredients I need to pull, and then push to the html page. They are listed as strIngredient1, strIngredient2, strIngredient3... Until 15. What I want to do is loop through each ingredient? I need some sort of dynamic variable I think. Totally not sure.

for (var x = 1; x < 16; x++) {
  if ((drinkResponse.drinks[0].strIngredient + x) !== null) {
    $("#drinkOutput").append($("<p class='drinkData'>Drink Ingredient" + x + " : " + (drinkResponse.drinks[0].strIngredient + x) + "</p>"))
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

This is the idea of what I want to do, but currently it returns NaN; when it should be returning a string.

JSON return:

drinks[0] {

  dateModified: "2017-09-02 16:38:19"

  idDrink: "11224"

  strAlcoholic: "Alcoholic"

  strCategory: "Ordinary Drink"

  strCreativeCommonsConfirmed: "No"

  strDrink: "Casino Royale"

  strDrinkAlternate: null

  strDrinkDE: null

  strDrinkES: null

  strDrinkFR: null

  strDrinkThumb: "https://www.thecocktaildb.com/images/media/drink/3qpv121504366699.jpg"

  strDrinkZH-HANS: null

  strDrinkZH-HANT: null

  strGlass: "Whiskey sour glass"

  strIBA: null

  strIngredient1: "Gin"

  strIngredient2: "Lemon juice"

  strIngredient3: "Maraschino liqueur"

  strIngredient4: "Orange bitters"

  strIngredient5: "Egg yolk"

  strIngredient6: null

  strIngredient7: null

  strIngredient8: null

  strIngredient9: null

  strIngredient10: null

  strIngredient11: null

  strIngredient12: null

  strIngredient13: null

  strIngredient14: null

  strIngredient15: null

  strInstructions: "In a shaker half-filled with ice cubes, combine all of the ingredients. Shake well. Strain into a sour glass."

  strInstructionsDE: "In einem Shaker, der halb mit Eiswürfeln gefüllt ist, alle Zutaten vermengen. Gut schütteln. In ein Sour Glas abseihen."

  strInstructionsES: null

  strInstructionsFR: null

  strInstructionsZH-HANS: null

  strInstructionsZH-HANT: null

  strMeasure1: "2 oz "

  strMeasure2: "1/2 oz "

  strMeasure3: "1 tsp "

  strMeasure4: "1 dash "

  strMeasure5: "1 "

  strMeasure6: null

  strMeasure7: null

  strMeasure8: null

  strMeasure9: null

  strMeasure10: null

  strMeasure11: null

  strMeasure12: null

  strMeasure13: null

  strMeasure14: null

  strMeasure15: null

  strTags: null

  strVideo: null

}

3
  • Just to understand the logic: drinkResponse.drinks is a list of drinks and each has a fixed amount of ingredients strIngredient, which however are not provided in the object itself as a list yet as a string property? Would you mind providing a sample object? Commented Feb 8, 2020 at 4:36
  • I've added the JSON return to the original question. Commented Feb 8, 2020 at 5:03
  • Thanks. You should try, if you can/have access to it, to turn those numerical lists (strMeasure14, strIngredient15) into arrays. Else the structure is kinda limited/flawed. It would also make it more simple to access it. Commented Feb 8, 2020 at 8:45

1 Answer 1

2

You are trying to append to the variable name with the value of x. You're close, but you need to use bracket notation rather than dot notation.

For example, if x is currently 5, you can get the value of strIngredient5 with drinkResponse.drinks[0]['strIngredient'+x] or drinkResponse.drinks[0][`strIngredient${x}`].

As Lain pointed out, you can also use Object.keys to enumerate all of the keys on the object, then filter for only the keys which start with strIngredient:

// get all keys on the drink
const allKeys = Object.keys(drinkResponse.drinks[0]);

// now filter for only keys starting with `strIngredient`
const ingredients = allKeys.filter(key => key.startsWith('strIngredient'));

for (const i=0; i<ingredients.length; i++) {
    $("#drinkOutput").append($("<p class='drinkData'>Drink Ingredient" 
    + i + " : " + (drinkResponse.drinks[0][ingredients[i]]) + "</p>"))
}

Note that this may not preserve the order, but you can preserve the order by combining the first two examples:

for (const i=0; i<ingredients.length; i++) {
    $("#drinkOutput").append($("<p class='drinkData'>Drink Ingredient" 
    + i + " : " + (drinkResponse.drinks[0][`strIngredient${i}`]) + "</p>"))
}
Sign up to request clarification or add additional context in comments.

2 Comments

If that is the case he should also loop though the all keys starting with strIngredient instead of having a fixed loop from 1 to 15.
@gcochard I tried that, and now my response is undefined of undefined

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.