2

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 do this? In Python, this is extremely easy -- just a couple lines of code. However, I got super confused after seeing related posts in JS. Any help is greatly appreciated!

(PS: Please let me know if I need to "import" anything. Any references will be very helpful too!)

1 Answer 1

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:

import CSV from 'comma-separated-values';

const csv = new CSV(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);
  });
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.