简体   繁体   English

Javascript forEach 不适用于 json

[英]Javascript forEach is not working for json

I have a ajax returning data some times like我有一个 ajax 有时会返回数据

{
    "results": [{
        "symbol": "AppConomy",
        "Name": null,
        "PriceSales": null
    }]
}

for above my forEach function is working fine but when same data is returning对于上面我的 forEach 函数工作正常但是当返回相同的数据时

 {
     "results": {
         "symbol": "AppConomy",
        "Name": null,
        "PriceSales": null
    } 
}

my forEach function not working我的 forEach 函数不起作用

 $.get(url, function(data){
        var x =data['results'];
        x.forEach(function logArrayElements(element, index, array) {
           $(self).append('<button class="tag-format" title="'+array[index].Name+'"  style="color:#fff;background-color:rgb(0,151,216);border:1px solid;border-radius:10px;">&nbsp;'+ array[index].symbol +" - "+ array[index].PriceSales +'&nbsp;</button>');
        });
     });

That's because your JSON isn't an array.那是因为您的 JSON 不是数组。 You can easily check beforehand using Array.isArray() .您可以使用Array.isArray()轻松预先检查。 You should also be using getJSON if the data you are retrieving is in fact JSON.如果您检索的数据实际上是 JSON,您也应该使用getJSON

$.getJSON(url, function(data) {
    var x = data.results;
    if(Array.isArray(x)) {
        x.forEach(function logArrayElements(element, index, array) {
            $(self).append('<button class="tag-format" title="' + array[index].Name + '"  style="color:#fff;background-color:rgb(0,151,216);border:1px solid;border-radius:10px;">&nbsp;' + array[index].symbol + " - " + array[index].PriceSales + '&nbsp;</button>');
        });
    } else {
        $(self).append('<button class="tag-format" title="' + x.Name + '"  style="color:#fff;background-color:rgb(0,151,216);border:1px solid;border-radius:10px;">&nbsp;' + x.symbol + " - " + x.PriceSales + '&nbsp;</button>');
    }
});

Your forEach is for iterating over an array.您的forEach用于迭代数组。 In your second JSON, there is no array to iterate over, so you need to call the function directly on data['results'] :在您的第二个 JSON 中,没有要迭代的数组,因此您需要直接在data['results']上调用该函数:

$.get(url, function(data){
  var x = data['results'],
      addButton = function(item) {
        $(self).append('<button class="tag-format" title="'+array[index].Name+'"  style="color:#fff;background-color:rgb(0,151,216);border:1px solid;border-radius:10px;">&nbsp;'+ array[index].symbol +" - "+ array[index].PriceSales +'&nbsp;</button>');
      };
  if(Array.isArray(x)) {
    x.forEach(function logArrayElements(element, index, array) {
      addButton(array[index]);
    });
  } else {
    addButton(x);
  }
});

Javascript object is not an Array. Javascript 对象不是数组。

Html html

<div id="results"></div>

Javascript Javascript

var firstArray = {
"results": [{
    "symbol": "AppConomy",
    "Name": null,
    "PriceSales": null
}]};

var secondArray =  {
 "results": {
     "symbol": "AppConomy",
    "Name": null,
    "PriceSales": null
}};

//Result: 1
$("#results").append("<span>FirstArray result: " + firstArray['results'].length + "</span><br/>");

//Result: undefined
$("#results").append("<span>FirstArray result: " + secondArray['results'].length + "</span><br/>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM