If you are not too worried about file size, it might be easier for you to store the data as a JS object in another file and import it into your own. Either synchronously or asynchronously using the syntax <script src="countries.js" async></script> . Saves you the need to import a file and analyze it.
However, I can understand why you do not want to rewrite 10,000 entries, so here was written the basic object-oriented csv parser that I wrote.
function requestCSV(f,c){return new CSVAJAX(f,c);}; function CSVAJAX(filepath,callback) { this.request = new XMLHttpRequest(); this.request.timeout = 10000; this.request.open("GET", filepath, true); this.request.parent = this; this.callback = callback; this.request.onload = function() { var d = this.response.split('\n'); var i = d.length; while(i--) { if(d[i] !== "") d[i] = d[i].split(','); else d.splice(i,1); } this.parent.response = d; if(typeof this.parent.callback !== "undefined") this.parent.callback(d); }; this.request.send(); };
What can be used as follows:
var foo = requestCSV("csvfile.csv",drawlines(lines));
The first parameter is the file relative to the position of your html file in this case. The second parameter is an additional callback function that runs when the file is fully loaded.
If your file has non-separating commmas, then it will not cope with this, since it simply creates 2d arrays, splitting them into returns and commas. You may want to look at the regular expression if you need this functionality.
//THIS works "1234","ABCD" \n " !@ Β£$" \n //Gives you [ [ 1234, 'ABCD' ], [ ' !@ Β£$' ] ] //This DOESN'T! "12,34","AB,CD" \n " !@ ,Β£$" \n //Gives you [ [ '"12', '34"', '"AB', 'CD' ] [ '" !@ ', 'Β£$' ] ]
If you do not use OO methods; they create a new object (for example, a number, a string, an array) with their local functions and variables through the constructor function. Very handy in certain situations. This function can be used to download 10 different files with different callbacks at the same time (depending on your level of love csv!)