Search code examples
javascriptjqueryarraysforeachgetjson

Loop through an array of urls and pass them to get_json, return the new array of datas


I can't figure how to loop through an array of urls and pass them to getJSON, to finally return the new array of datas.

var data_num=[];
$.each(arr_urls, function( index, value ) {          
    $.getJSON(arr_urls[index], function (data) {});
    data_num.push(data);
});
alert(data_num);

Solution

  • var data_num=[];
    $.each(arr_urls, function( index, value ) {
    
      $.getJSON(arr_urls[index], function (data) {
          data_num.push(data);
          alert(data_num);
      });
    });
    

    Put your pusher and alert in the callback like this.