I am working with the csv-parse module in npm to download a CSV file over Ajax. I want to read it into an array of objects.
Here is the CSV file that I'm working with:
val,row_id,date,row_name
19378.02,10Q,2013-10-01,NHS Oxfordshire
14104.81,01M,2014-01-01,NHS North Manchester
11130.65,09D,2014-09-01,NHS Brighton & Hove
3611.32,08J,2014-11-01,NHS Kingston
And here is my code:
$.ajax({
type: "GET",
url: _this.globalOptions.numeratorUrl,
error: function() {
// show error
},
success: function(data) {
parse(data,
{columns: ['val','row_id','date','row_name'] },
function(err, output){
console.log('parsed', output);
}
);
}
This works, but there are two things I would like to improve:
- By default it includes the header row, so
output[0]looks like this:{ 'val': 'val', 'row_id': 'row_id', ...}. Is there some way I can automatically skip the header row? - I don't like the fact that I have to define
columnsexplicitly in order to produce an array of objects. The name and the ordering of the columns may vary. Is there some way I can automatically use the values from the header row, whatever they are?