0

I am using VS Code and running Javascript. Beside VS Code I have installed Node in my computer. I am using windows. I have csv file in the same folder as my .js file.

How do I save the content of the csv file into an array?

Let's say my csv file have this content and I want to store it as an array variable A:

101,MS,100000 102,LS,80000 103,TS,60000 104,VB,200000 105,PB,180000 106,AB,160000

Jon B
  • 51,025
  • 31
  • 133
  • 161
k.dkhk
  • 481
  • 1
  • 11
  • 24
  • I have tried to follow many instructions on the net but I haven't been able to succeed. Maybe because I have a background in Math and not Computer Science. So while I can setup algorithms for numerical purposes, I have difficulty understanding some very basics of Computer Science. The latest article I followed is this: http://www.dotnetcurry.com/nodejs/1204/read-csv-file-using-nodejs – k.dkhk Jul 13 '17 at 18:05
  • Use `Split()`. Works like this: `var A = yourCsvFileAsAString.split(",")`. To read your file as a string, get it with a XMLHttpRequest. Links: https://www.w3schools.com/jsref/jsref_split.asp, https://stackoverflow.com/questions/14446447/javascript-read-local-text-file – Dwadelfri Jul 13 '17 at 18:11

1 Answers1

2

Since you have node installed, you could use a npm package https://www.npmjs.com/package/csv-string to do so.

They have listed out all the ways that can be used to generate an array.

Hardik Jain
  • 184
  • 8