Skip to content
Advertisement

How to read data From *.CSV file using JavaScript?

My CSV data looks like this:

JavaScript

How do you read this data and convert to an array like this using JavaScript?:

JavaScript

I’ve tried this code but no luck!:

JavaScript

Advertisement

Answer

NOTE: I concocted this solution before I was reminded about all the “special cases” that can occur in a valid CSV file, like escaped quotes. I’m leaving my answer for those who want something quick and dirty, but I recommend Evan’s answer for accuracy.


This code will work when your data.txt file is one long string of comma-separated entries, with no newlines:

data.txt:

JavaScript

javascript:

JavaScript

The following code will work on a “true” CSV file with linebreaks between each set of records:

data.txt:

JavaScript

javascript:

JavaScript

http://jsfiddle.net/mblase75/dcqxr/

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement