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?
2 Answers 2
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);
3 Comments
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);
}
javascriptand callscsv2Dictionary. Sounds like you've got what you want?