0

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.

ttt
  • 401
  • 2
  • 4
  • 17

3 Answers3

0

You have to put console.log inside your function instead of outside.

$.getJSON('/load4',
  function(data) {
    var datas = [];
    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);
  });
Vipin Kumar
  • 6,441
  • 1
  • 19
  • 25
0

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

Yairopro
  • 9,084
  • 6
  • 44
  • 51
-1

You have to create a variable datas first.

 var datas =[];   
 $.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);
Wilson
  • 329
  • 2
  • 11