In my code I use AJAX to fill an array and that's work perfectly but when I use length for this in outside its giving 0.
Look at this code and the comment show you exactly what I mean.
$.ajax({
url: "url",
type : "GET",
//contentType: "application/json",
//dataType: "json",
data : { firstDate : "date1",
secondDate : "date2"}
//student_name : $('#id_student_name').val()
}).then(function(data) {
var obj = JSON.parse(data);
obj.results.forEach(element => {
console.log(element.DATEEND);
res.push(element);
dates.push(element.DATESTART);
});
});
var days = ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'];
var i;
function min_date(all_dates) {
var min_dt = all_dates[0],
min_dtObj = new Date(all_dates[0]);
all_dates.forEach(function(dt, index){
if ( new Date( dt ) < min_dtObj)
{
min_dt = dt;
min_dtObj = new Date(dt);
}
});
return min_dt;
}
var d = [];
console.log(res.length + " "); // this give 0
for (var a = 0; a < res.length; a++) {
d[a] = res[a].DATESTART;
console.log(res[a].DATESTART);
}
console.log(d);//this is vide
console.log(res); //this give all element
.
.
.