0

I need to analyze data using JS, and display it on an HTML page. The data source is a CSV file, so I used the following code for extracting the data:

        var data;

        $(document).ready(function(){
        $("#csv-file").change(handleFileSelect);
        });

        function handleFileSelect(evt) {
            var file = evt.target.files[0];

            Papa.parse(file, {
                header: true,
                dynamicTyping: true,
                complete: function(results) {
                data = results;
                }
            }); 
        }

When I press F12 in Chrome - with breakpoint on the row following "data=results", I can see that "data" is an array with 50 objects - each contains a whole row from the CSV in a Key-Value format, where the "Key" is the header from the file and the "value" is the content.

The problem is that I can't access the data. For example, I tried writing:

var x = document.getElementById("one"); x.innerHTML=data[1].Date;

but there's an error on the second line... So - Does anyone can help me with how can I get access to the "data" variable? in which part of the code I need to insert the lines that tries to get the data?

Bramat
  • 979
  • 4
  • 24
  • 40
  • possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Ben Fortune Jul 31 '15 at 13:12
  • No, mine doesn't use AJAX... and if that solution can help me - I'll appriciate it if you can explain to me how to to do it... – Bramat Jul 31 '15 at 14:00
  • It's the same concept. Look at how they are using the data from the callback. Your `complete` method is their `success` method. – Ben Fortune Jul 31 '15 at 14:01
  • in the link there's a direct call to foo(), in my code the call depends on when I choose the file... so It's a bit hard for me to do those comprisons... I'm quite a beginner, I don't really know how to do it, sorry.. – Bramat Jul 31 '15 at 14:12
  • In short, you need to do all your logic inside the `complete` callback. – Ben Fortune Jul 31 '15 at 14:19

0 Answers0