Problem
Eine CSV Datei soll zeilenweise per Javascript eingelesen werden
A CSV file should be read line by line via javascript
Ansatz – Approach
Es wird die $.ajax-Funktion von JQuery verwendet. JQuery wird entsprechend http://jquery.com/download/ eingebunden.
We are using the $.ajax-Method of JQuery. JQuery will be embedded according to http://jquery.com/download/.
Lösung – Solution
$(document).ready(function() { $.ajax({ type: "GET", url: "data.txt", dataType: "text", success: function(data) {processData(data);} }); }); function processData(allText) { var allTextLines = allText.split(/\r\n|\n/); var headers = allTextLines[0].split(','); var lines = []; for (var i=1; i<allTextLines.length; i++) { var data = allTextLines[i].split(','); if (data.length == headers.length) { var tarr = []; for (var j=0; j<headers.length; j++) { tarr.push(headers[j]+":"+data[j]); } lines.push(tarr); } } // alert(lines); }
Uncaught SyntaxError: Unexpected token ;
in
for (var i=1; i<allTextLines.length; i++) {