0

I am having trouble outputting values from an array when I have pushed them through multiple ajax async calls.

I can output the main array in a console log but printing out the individual values I cannot. I also would like to sort the values high to low

$(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 Example

{"equity" : 5000} //File 1
{"equity" : 2000} //File 2
{"equity" : 10000} //File 3
6
  • 1
    Seems you either need to look at promises or at least call the next ajax inside the success of the previous. But why not call one file once? Commented Jun 5, 2019 at 7:40
  • Individual URLS coming from API Commented Jun 5, 2019 at 7:42
  • Possible duplicate of stackoverflow.com/questions/14220321/…. Commented Jun 5, 2019 at 7:43
  • Dupe of TWO - how to handle a return from async and how to run more than one async Commented Jun 5, 2019 at 7:44
  • Also why sort inside the loop? Commented Jun 5, 2019 at 7:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.