gpt4 book ai didi

javascript - 如何在 JavaScript 的 for 循环中创建动态变量以返回 json?

转载 作者:行者123 更新时间:2023-12-01 00:10:17 25 4
gpt4 key购买 nike

我调用的 API 有一部分我需要拉取成分,然后推送到 html 页面。它们被列为 strIngredient1、strIngredient2、strIngredient3...直到 15。我想要做的是循环遍历每种成分?我认为我需要某种动态变量。完全不确定。

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>

这就是我想要做的想法,但目前它返回 NaN;当它应该返回一个字符串时。

JSON 返回:

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

}

最佳答案

您正在尝试将值 x 附加到变量名称。您很接近,但您需要使用括号表示法而不是点表示法。

例如,如果x当前为5,您可以通过drinkResponse.drinks[0]['strIngredient'+x]获取strIngredient5的值drinkResponse.drinks[0][`strIngredient${x}`]

正如 Lain 指出的,您还可以使用 Object.keys 枚举对象上的所有键,然后仅过滤以 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>"))
}

请注意,这可能不会保留顺序,但您可以通过组合前两个示例来保留顺序:

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

关于javascript - 如何在 JavaScript 的 for 循环中创建动态变量以返回 json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60123904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com