Skip to content Skip to sidebar Skip to footer

Javascript .csv To Arrays

I have a .CSV file. It has 4 columns and thousands of rows. I want to have 4 arrays, one for each column. I recently started learning JavaScript. Can someone please tell me how to

Solution 1:

CSV can be tricky because cells can have escaped commas - comma-separated-values looks like a good choice since it is RFC 4180 compliant:

To install the package:

npm install comma-separated-values--save

And then to use it your app:

importCSVfrom'comma-separated-values';

const csv = newCSV(data, {header: true}).parse();

That will result in an array of arrays (of rows) - to transform that into an array of arrays (of columns):

const cols = [[],[],[],[]];
csv.forEach(row => {
  row.forEach((cell, idx) => {
    cols[idx].push(cell);
  });
})

Post a Comment for "Javascript .csv To Arrays"