Home >Web Front-end >JS Tutorial >How to Easily Parse CSV Data into JavaScript Arrays?
How to Parse CSV Data into Arrays Using JavaScript
Reading CSV (Comma-Separated Values) data and converting it into JavaScript arrays can be done with ease.
The CSV Data Format
The given CSV data adheres to the following format:
heading1,heading2,heading3,heading4,heading5 value1_1,value2_1,value3_1,value4_1,value5_1 value1_2,value2_2,value3_2,value4_2,value5_2 ...
Converting to Arrays
To convert this data into an array, you can leverage the $.csv.toObjects() function provided by the jQuery-CSV library. This function automatically maps the CSV data into objects.
Example
For the provided dataset, the code below will generate the desired array:
$.csv.toObjects(csv);
Output
The resulting array will be:
[ { heading1: "value1_1", heading2: "value2_1", heading3: "value3_1", heading4: "value4_1", heading5: "value5_1" }, { heading1: "value1_2", heading2: "value2_2", heading3: "value3_2", heading4: "value4_2", heading5: "value5_2" } ]
Note
The above is the detailed content of How to Easily Parse CSV Data into JavaScript Arrays?. For more information, please follow other related articles on the PHP Chinese website!