3

I'm new to JavaScript and jQuery. I'm trying to parse CSV file where delimiter is not standard sign of comma (,), but something else(;).

I'm using the jQuery function $.csv.toArrays(csv, options, callback). I'm aware that delimiter is supposed to be set in options, but I'm having hard time figuring how exactly to do this.

Thank you very much.

Evan Plaice
  • 13,944
  • 6
  • 76
  • 94
Antea
  • 177
  • 1
  • 4
  • 10

4 Answers4

4

Try with:

var options={"separator" : ";"};
$.csv.toArrays(csv, options);
Evan Plaice
  • 13,944
  • 6
  • 76
  • 94
juvian
  • 15,875
  • 2
  • 37
  • 38
  • FYI, the callback us optional unless you need to specify a callback function to run after the parsing is complete. – Evan Plaice Jan 21 '16 at 12:36
2

According to the documentation you can override the delimiter in the options object like this:

$.csv.toArrays(csv, {'separator':';'}, callback);
Evan Plaice
  • 13,944
  • 6
  • 76
  • 94
Reeno
  • 5,720
  • 11
  • 37
  • 50
0

Assuming you're referring to jquery-csv, then according to the documentation available here you need to use the following:

$.csv.toArrays(csv, { separator: ';' });
Evan Plaice
  • 13,944
  • 6
  • 76
  • 94
Phylogenesis
  • 7,775
  • 19
  • 27
0
var csv = "Joe;23;USA\n" + 
          "Attila;24;Hungary\n" + 
          "Li;20;China",
    options = { separator: ";" };

$.csv.toArrays(csv, options, function(err, data){
    console.log(data);
});

The data argument of the callback will be an array of arrays:

[["Joe","23","USA"],["Attila","24","Hungary"],["Li","20","China"]]

Here is the jsfiddle.

kol
  • 27,881
  • 12
  • 83
  • 120