I have following block of code
var cars = $('#mypage').data('carsdata');
if (cars == null) {
var clientId = $("input[name='ClientId']").val();
worksheet.ajax.execute('GET','/Cars/GetData/', { clientId: clientId }, function (res) {
if (res == "") {
return;
}
cars = res.Owners.filter(function (e) {
if (e.Email) {
return true;
}
return false;
}).map(function (e) {
return { key: e.Id, label: e.Email };
});
console.log("1" +cars.length); // cars lenght is 3
});
console.log("2" +cars.length); // cars is null
maybe this is too much code but the problem is that cars variable on last line is always null even though I have if statement where I checking null and in that case I'm populating that variable, where in
console.log("1" +cars.length);
cars length is 3, so it's not null. Why this cars.length is not replicated on
console.log("2" +cars.length);
is everything ok with this map function
in a way that I'm correctly populating cars variable?
Update:
when cars is correctly loaded with data- attribute I'm getting data in following format
[Object { key=101, label="email@email.com"}, Object { key=22, label="email2@email.com"}, Object { key=442, label="email43@email.com"}]