0

Looking to use the data in a CSV to create a network graph. Kinda stuck at the first hurdle as I want to create an array of arrays from the CSV using PapaParse, however can't seem to push the data from papaParse into my array, all I get is an empty array returned in my console. Can anyone tell me what I'm doing wrong?

var dirtyNodeData = [];
Papa.parse("http://example.com/tmp/csvfile.csv", {
    download: true,
    header: false,
    complete: function(results) {
        dirtyNodeData.push(results.data);
    }
});

console.log(dirtyNodeData);                 
ThallerThanYall
  • 197
  • 2
  • 14

1 Answers1

2

The Papa.parse function is asynchronous, so you'll need to handle receiving data when you receive the data inside your complete function rather than just after calling parse. For example, you could do this:

var dirtyNodeData = [];
Papa.parse("http://example.com/tmp/csvfile.csv", {
    download: true,
    header: false,
    complete: function(results) {
        dirtyNodeData.push(results.data);
        console.log(dirtyNodeData);  
        }
    });

i.e. you can add handler code after you push the data into your array. You could also create another function called postReceiveCSV() which is called in place of the console.log, in which you can add anything you need to do after the data is successfully received.

If that isn't the issue, then check that the results object is what you expect.

Alex Blundell
  • 2,084
  • 5
  • 22
  • 31