无法从数组JavaScript输出值,然后按高到低排序

时间:2019-06-05 07:38:41

标签: javascript arrays sorting

在通过多个ajax异步调用推送它们时,从数组输出值时遇到麻烦。

我可以在控制台日志中输出主数组,但不能打印出我无法打印的单个值。我也想将值从高到低排序

$(document).ready(function() {
  var callsArray = [];

  $.ajax({
    type: 'GET',
    url: "js/equity-one.json",
    dataType: "json",
    traditional: true,
    async: true,
    success: function(data) {
      $.each(data, function(i, item) {
        callsArray.push(item);
        $(".equity").append("<tr><td>" + item + "</td></tr>");
      });
    }
  });

  $.ajax({
    type: 'GET',
    url: "js/equity-two.json",
    dataType: "json",
    traditional: true,
    async: true,
    success: function(data) {
      $.each(data, function(i, item) {
        callsArray.push(item);
        $(".equity").append("<tr><td>" + item + "</td></tr>");
      });
    }
  });

  $.ajax({
    type: 'GET',
    url: "js/equity-three.json",
    dataType: "json",
    traditional: true,
    async: true,
    success: function(data) {
      $.each(data, function(i, item) {
        callsArray.push(item);
        $(".equity").append("<tr><td>" + item + "</td></tr>");
      });
    }
  });
  console.log(callsArray); // This outputs what has been pushed
  var arr = callsArray.sort();
  for (let x = 0; x < arr.length; x++) { //Not sure what to do here

    console.log(arr[x]);
  }

});

JSON示例

{"equity" : 5000} //File 1
{"equity" : 2000} //File 2
{"equity" : 10000} //File 3

0 个答案:

没有答案