In the following code, I'm wondering why my console.log(dataArray) displays an empty array. I think the console.log may be called before the ajax request is finished pulling data. If this is the case, is there any way to wait for the ajax request to finish before calling console.log ?
var dataArray = [];
var gdpAPI = "https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json";
$.getJSON( gdpAPI, {
format: "json",
async: false
})
.done(function( data ) {
yearsData = data['data']
$.each(yearsData, function(i){
dataArray.push(yearsData[i]);
})
});
console.log(dataArray);
Thank you in advance. Wyatt