简体   繁体   中英

How do I store an array of objects that are a result of an ajax query pulling data from JSON in javascript?

I was able to successfully retrieve my JSON objects using the code below. When I log the variable "value2", I can see the values in my console. However I am unable to populate my "arraytest" with value2. When I print the value of arraytest in the console after the function runs, the array is empty.

    var accessURL = "https://lots-of-holes.firebaseio.com/.json";
    var arraytest= [];
    i=0;
    $.getJSON(accessURL, function(data){
    $.each(data, function (index, value) {
        $.each(value, function (index2, value2) {               
                arraytest[i++] = value2;
                console.log(value2);
        });
      });
    });
    console.log(arraytest);

It may be something to do with the query running slower than my javascript but I am not sure how to handle this. If you think it is something else and would like to still help, please message me and I will give you a link to my sourcecode. I am new to javascript and ajax.

The JSON is below:

{"-37 932570096604465 * 101 68831543328787":{"frequency":2.0,"lat":-37.932570096604465,"lon":101.68831543328787},"-79 14830348215878 * -147 67984075199726":{"frequency":2.0,"lat":-79.14830348215878,"lon":-147.67984075199726},"-9 566696357885519 * -86 19132124619011":{"frequency":2.0,"lat":-9.566696357885519,"lon":-86.19132124619011},"23 175286370699936 * -14 694988385655307":{"frequency":2.0,"lat":23.175286370699936,"lon":-14.694988385655307},"25 686572941892038 * -54 7326350327119":{"frequency":2.0,"lat":25.686572941892038,"lon":-54.7326350327119},"39 2538061 * -76 7143967":{"frequency":10.0,"lat":39.2538061,"lon":-76.7143967},"39 2538512 * -76 7144418":{"frequency":2.0,"lat":39.2538512,"lon":-76.7144418},"39 2543882 * -76 7132241":{"frequency":2.0,"lat":39.2543882,"lon":-76.7132241},"39 2543986 * -76 7133143":{"frequency":2.0,"lat":39.2543986,"lon":-76.7133143},"52 0353479004271 * 147 31906570837452":{"frequency":2.0,"lat":52.0353479004271,"lon":147.31906570837452},"7 568992516106547 * 37 95350231539729":{"frequency":2.0,"lat":7.568992516106547,"lon":37.95350231539729},"78 51991220059591 * 69 61956909362064":{"frequency":2.0,"lat":78.51991220059591,"lon":69.61956909362064}}

The $.getJSON is an async function. So the console.log(arraytest) is running before that the arraytest is populate.

You can try this:

    var accessURL = "https://lots-of-holes.firebaseio.com/.json";
    var arraytest= [];
    i=0;
    $.getJSON(accessURL, function(data){
    $.each(data, function (index, value) {
        $.each(value, function (index2, value2) {               
                arraytest[i++] = value2;
                console.log(value2);
        });
      });
    console.log(arraytest);
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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