1

For a script I am working I keep getting an array filled with 'undefined' instead of the actual values, I have a CSV file which im reading with $.ajax, I am trying to split the entire CSV into arrays based on the header row.
The code I have now:

$.ajax({
    type: "GET",
    url: "airport.txt",
    dataType: "text",
    success: function(data) {
        lines = processData(data);
        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(headers[j]+":"+data[j]);
                    }
                    lines.push(tarr);
                }
            }
            lines.map(function(d){
                country.push(d.headers);
            });
            console.log(country);
        }
    }
});

I think the issue lies within the country.push(d.headers); I've tried several other options for the d.headers such as d.lines d.headers[0] d.country and d.data.

What I want is for country to be filled with headers[0] which is the column labeled 'Country Code', but as soon as I change d.headers into d.headers[0] I get Uncaught TypeError: Cannot read property '0' of undefined
What is going wrong here and how can I best approach filtering the CSV file by column?

Edit:
With help of the plugin in the comments I have managed to make it work. Should anyone else run into this, here is the code snippet I've used to make it work for me, it could probably be better but I'm not really advanced in JavaScript.

$(document).ready(function(){
  //Read CSV
  $.ajax({
    type: "GET",
    url: "airport.txt",
    dataType: "text"
  })
  .done(function(data){
    //process CSV Data
    processCsv(data);
  })
  .fail(function(){
    alert("Error: Failed to load data");
  });
});

function processCsv(data){
  //Read CSV Data into multidimensional Array
  var array = $.csv.toArrays(data);
  //Put headers in own array
  var headers = array.splice(0,1);
  //Make each header its own value
  var headerString = headers.toString();
  headers = headerString.split(',');
  //separate all array values
  var arrayString = array.toString();
  array = arrayString.split(',');

  return { header:headers, values:array};
}
  • [This CSV parser](https://github.com/evanplaice/jquery-csv) may interest you... – Louys Patrice Bessette Oct 25 '16 at 14:01
  • Thanks @LouysPatriceBessette it seems it might do what I need it to do, sadly though i get the same issue others have reported to the author, line 170 gets a csv.replace is not a function, do you know any other ways I might be able to solve the issue at hand? – Development SideShow Media Oct 26 '16 at 07:32
  • mmm... Sorry for that. Maybe try [another](http://papaparse.com/)? Because trying to code your own parser maybe a great coding exercise. But you probably will encounter the same issues as other parsers. If you still want to achieve it yourself, then post a sample of your CSV... And we'll try. ;) – Louys Patrice Bessette Oct 26 '16 at 18:25
  • @LouysPatriceBessette I've managed to get it to split the CSV into the arrays I can use to read, just have to make it so I can use the arrays in the right area now, if I get stuck on that I'll make a new question, thanks a lot for the help :) – Development SideShow Media Oct 27 '16 at 06:59
  • The abyss of the *undefined* is awaiting you... ;) lolll. But cool. Feel free to contact me via [FB](https://www.facebook.com/bessetteweb/) – Louys Patrice Bessette Oct 27 '16 at 07:05

0 Answers0