I want to create a simple function that reads some json, does some simple processing, and returns an array as a result.
I currently have the code below. However, the function always returns [].
If I move temps to the global scope, it first returns [], then the second time it's called the data is added, and the third time it's called it's double the length etc.
function getTempData() {
var temps = [];
$.getJSON('temp.json', function(data) {
data = data.observations.data;
for(var i = 0; i < data.length; i++) {
year = parseInt(data[i].aifstime_utc.substr(0, 4));
month = parseInt(data[i].aifstime_utc.substr(4, 2));
day = parseInt(data[i].aifstime_utc.substr(6, 2));
hour = parseInt(data[i].aifstime_utc.substr(8, 2));
min = parseInt(data[i].aifstime_utc.substr(10, 4));
time = Date.UTC(year, month, day, hour, min);
temps.push([time, data[i].air_temp]);
}
});
return temps;
}
looking at some other questions, it seems this may have something to do with asynchronism? Is there a way to get it to load synchronously, I would like to graph the data in a highchart.