1

I'm using papaparse to parse a local csv file using the following:

    var display_links = [];
Papa.parse(file_links, {
    header: true,
    download: true,
    dynamicTyping: true,
    complete: function (results) {
        results.data.push(display_links)
    }
});
console.log(display_links)

How do I push the results of the parse into a local array so I can use it in other functions/processes?

console.log(display_links)

Returns an empty array.

stephen.webb
  • 175
  • 2
  • 13

2 Answers2

2

In your complete function you can replace results.data.push(display_links) with a reassignment: display_links = results.data;. It seems you have it backwards.

If you have declared display_links outside that function scope you should have access to the results later on. So the easy solution is to just pull it out of the local scope.

If that's not feasible, please provide a more complete example.

let display_links = [];

function doSomethingWithDisplayLinks(results) {
  display_links = results;
}

console.log('display_links before:', display_links);
doSomethingWithDisplayLinks(['http://www.google.com']);
console.log('display_links after:', display_links);
manonthemat
  • 6,101
  • 1
  • 24
  • 49
1

Facing a similar issue with Papaparse or it is my current knowledge with the library and callbacks. Though I can point you in the direction where you might be able to find an answer for yourself.

First thing, your display_links comes up an empty array because when parsing file via Papaparse, the complete is an asynchronous callback function which means your assignment of display_links inside the callback happens asynchronously after the file that you selected is completly processed. So when you log your variable, it is not yet assigned the result. In short this would not work.

Now to solve your problem, the only answer that I have found is that whatever plans you have for display_links, you can put them in a function and from inside complete, call that function with display_links as argument. That way, your function would be executed with the results of the parse.

Look up into Asynchronous JS and how callback works to find a more optimal solution for yourself if this does not work for you.

Side note - My issue is, I wanted to abstract the parsing and other related actions inside a function and once the file would have been read / parsed, that function would return the end result back. This last part of retunring the end result, is something I am not able to accomplish as I think you can only move forward and call another function from the complete callback and returning does not work :/

Ref -

Tannu
  • 135
  • 3
  • 13
  • Found a way to make this work for me. The way I did it was, I passed in a callback to the first function uses Papaparse to parse and then upon completion, just called that from the `complete`. Not sure if it is the best but that worked for me, sorta :P – Tannu Mar 11 '19 at 10:06
  • Thank you - I hadn't appreciated the asynchronous angle. Now when I push results.data into the display_links array and then call a console.log from a secondary function, it logs the array properly. – stephen.webb Mar 11 '19 at 13:29