1

I am trying to import a .csv file in my JavaScript and trying to save it in an array. I got the below sample while searching on stackoverflow.com to use jQuery-CSV library's function called csv2Dictionary() to do so.

var data = $.csv2Dictionary(csv):

In above line of code "data" will give me my array, but can some one help me know before I get to this step, how can I call csv2Dictionary() from my JavaScript?

4
  • 7
    Above code is javascript and calls csv2Dictionary. Sounds like you've got what you want? Commented Sep 13, 2012 at 14:15
  • 2
    You can directly call it without any problems. jQuery is a framework that just gives an easier way of writing Javascript code. Commented Sep 13, 2012 at 14:15
  • 1
    that line is javascript, just use it like that. But make sure you have included the library (jQuery-CSV) you are using Commented Sep 13, 2012 at 14:15
  • 4
    If you meant call it, without using jQuery, just javascript, you can't, you need to have jQuery loaded. Otherwise as stated, jQuery is javascript, just a framework built on it. Commented Sep 13, 2012 at 14:18

2 Answers 2

1

What do you exactly mean "call it from my javascript"?

The answer is easy: do it ;)

Just make sure that jQuery and jQuery-CSV are loaded before you try to call the function. Then you can easily do something like this:

var csv;
// ...
// Create your CSV
// ...
$.csv2Dictionary(csv);
Sign up to request clarification or add additional context in comments.

3 Comments

Well my real concern is that I have my data in say data.csv file. How do I put the data in var csv so that when the jquery function $.csv2Dictionary(csv) is called it knows which data to be modified? I hope I am not confusing you all further. Thanks in advance.
Ah, you want to access the File on your Computer via Javascript? Maybe you can use the Javascript File API, but I'm not sure...
Maybe this little tutorial can help you: html5rocks.com/en/tutorials/file/dndfiles
0
    readTheFileWhenLoaded = function(){
        $('#inputTypeFileInHTML').change(function(event){
            readTheFile(this.files[0]);
            $('#inputTypeFileInHTML').val(null);
        });
    }

     readTheFile= function(file){
        var fileContent;
          var r = new FileReader();
          r.onload = function(e) {
              fileContent= e.target.result;
              fileContentAsStringJavaScriptVar(fileContent);
          }
          r.readAsText(file);
    }

    fileContentAsStringJavaScriptVar= function(fileContent){
        console.log(fileContent);
         $.csv2Dictionary(fileContent);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.