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.
我能够使用下面的代码成功检索JSON对象。当我记录变量“value2”时,我可以看到控制台中的值。但是,我无法用value2填充“arraytest”。当我在函数运行后在控制台中打印arraytest的值时,数组是空的。
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.
这可能与查询运行速度比javascript慢有关,但我不确定如何处理这个问题。如果你认为这是另外的事情,并想继续帮助,请给我留言,我将给你一个链接到我的源代码。我对javascript和ajax很陌生。
The JSON is below:
JSON是以下:
{"-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}}
1 个解决方案
#1
1
The $.getJSON
is an async function. So the console.log(arraytest)
is running before that the arraytest is populate.
美元的。getJSON是一个异步函数。因此,console.log(arraytest)在填充arraytest之前运行。
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);
});
#1
1
The $.getJSON
is an async function. So the console.log(arraytest)
is running before that the arraytest is populate.
美元的。getJSON是一个异步函数。因此,console.log(arraytest)在填充arraytest之前运行。
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);
});