从网络响应中获取值

从网络响应中获取值

我具有此功能及其从网络响应中获取值的功能,并设法显示输出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);
  }
}


电流输出


  配方无法禁用,在以下子配方中使用:
  (1)value_2


我想要的是


  配方无法禁用,在以下子配方中使用:
  (0)值_1(1)值_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 - Javascript从网络响应中获取值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59871015/

10-10 09:30