gpt4 book ai didi

Javascript 从网络响应中获取值

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

我有这个函数以及它从网络响应中获取值的作用,并且我设法显示输出 0: {recipeName: value_1}1: {recipeName: value_2} 。但现在的问题是我想将结果与警报消息一起显示。知道如何使警报消息仅显示一次但结果循环显示吗?

function (e) {
var returnedData = JSON.parse(e.responseText);
console.log(returnedData);

if(returnedData.length != 0){
for (var x=0; x < returnedData.length; x++ ){
var listOfRecipe = '('+[x]+') ' + returnedData[x]['recipeName'];
}
alert("Recipe cannot be deactivated, it is used in the following sub recipes: " + listOfRecipe);
}
}

电流输出

Recipe cannot be deactivated, it is used in the following sub recipes: (1) value_2

我想要什么

Recipe cannot be deactivated, it is used in the following sub recipes: (0) value_1 (1) value_2

最佳答案

在循环之外声明您的食谱列表。然后将您的代码更改为这样

function (e) {
var returnedData = JSON.parse(e.responseText);
var listOfRecipe = ''
console.log(returnedData);

if(returnedData.length != 0){
for (var x=0; x < returnedData.length; x++ ){
listOfRecipe += '('+[x]+') ' + returnedData[x].recipeName;
}
alert("Recipe cannot be deactivated, it is used in the following sub recipes: " + listOfRecipe);
}
}

关于Javascript 从网络响应中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59871015/

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