I have this strange scope issue with the while loop below. If I place console.log inside both the function and while loop I get the result I'm looking for, but for the code below I get nothing at all. It seems that the .push method is not pushing the values into the arrays CCY and dts. Any ideas how I can fix this issue?
var CCY=[];
var dts=[];
var start = new Date(x);
var end = new Date(y);
while(start < end){
var newDate = start.setDate(start.getDate() + 1);
start = new Date(newDate);
var d = (start.toISOString().split('T')[0]);
var JSONItems=[];
$.getJSON("http://api.fixer.io/"+d, function(data){
JSONItems = data;
CCY.push([JSONItems.rates.USD]);
dts.push([JSONItems.date])
});
}
console.log("Date= "+dts);
console.log("Rate= "+CCY);