javascriptmassive

Why 'datas' array is always empty?


My code

 $.getJSON('/load4',
    function (data) {
        for (var i = 0; i < data.length; i++) {
            var counter = data[i];
            datas.push([counter.Id, counter.login.substr(0, 25),'3', counter.hash , 'MA']); 
        }
    });
console.log("Datas " + datas);

I'm trying to fill my array like [[a,b],[a,c]], but can't.

datas[i] = [counter.Id, counter.login.substr(0, 25),'3', counter.hash , 'MA'];

Also doesn't work.


Solution

  • That's because you are loading your JSON asynchronously. Which means that the log runs before the callback function passed as parameter to getJSON.