0

I would know how to read a two columned .csv file (it is in .txt format) without header like this:

52,10
53,23
66,45
..,..
34,67

and store the first and the second value of each line respectively into an 2D array var coords = [[52,10],[53,23],[66,44],....,[34,67]];

in order to work with them in other functions. they are about 500,000 lines, is there also a limit to store it in array?

This code, I found it and modified it but it loads the csv .txt file and display it in browser.

JS:

document.getElementById('polygon').onchange = function(){


var file = this.files[0];

var reader = new FileReader();

reader.onload = function(progressEvent)
{

// Read line-by-line
var lines = this.result.split('\n');

for(var line = 0; line < lines.length; line++){

document.write(lines[line]);

  }
};

reader.readAsText(file);

};

Html

<!DOCTYPE html>
<html>
    <head>
        <title>Page Title</title>
    </head>
    <body>
    <div>
    Upload your polygon's file:
       <br>
       <br>
       <input type="file" name="file" id="polygon">
    </div>
    </body>
</html>

Then I can do for-loop to print or do what I want with these 2D array elements

Note: without jquery, just pure JS

user9322960
  • 135
  • 8

1 Answers1

0

I cannot read the mind of whoever downvoted but for me this is a pure javascript question, not opencsv or even csv because you did that legwork already. Or it can be that your question seems too homeworkish but you at least are providing code examples that show work so I doubt that.

You need to create an array of size lines.length and then for each element of line set the new array to lines[line].split(',').

Give that a try :)

Scott Conway
  • 975
  • 7
  • 13
  • I tried this: https://jsfiddle.net/6xy1rdq8/ but unfortunately I need so much knowledge in order to work correctly :( – user9322960 Mar 07 '18 at 21:46
  • Did not know about the push command (I do not do much javascript). try modifying your code to look something like Sirko's answer from https://stackoverflow.com/questions/11345954/push-a-two-dimensional-array and see if that works. – Scott Conway Mar 08 '18 at 19:10