1

I'm trying to print the array (lines) but it's only possible inside the function (process data) but if i call it outside it's undefined.

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "data.txt",
        dataType: "text",
        success: function(data) {processData(data);}
     });
});


var lines = [];

function processData(allText) {
    var allTextLines = allText.split(/\r\n|\n/);
    var headers = allTextLines[0].split(',');


    for (var i=1; i<allTextLines.length; i++) {
        var data = allTextLines[i].split(',');
        if (data.length == headers.length) {

            var tarr = [];
            for (var j=0; j<headers.length; j++) {
                tarr.push(data[j]);
            }


            lines.push(tarr);
        }
    }

}

 alert(lines);
jeremy
  • 13
  • 2

1 Answers1

2

You are printing it before waiting for the function call. Do it as follows:

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "data.txt",
        dataType: "text",
        success: function(data) {processData(data); alert(lines);}
     });
});
Joanvo
  • 5,677
  • 2
  • 25
  • 35