Created
January 15, 2018 16:57
-
-
Save elsheikh21/0c559c6e2fcd8b8981ad1f2a48b49d6e to your computer and use it in GitHub Desktop.
Parsing CSV file client side, sending json object to Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function uploadCSV(csv) { | |
var lines = csv.split("\n"); | |
var result = []; | |
var headers = lines[0].split(","); | |
for (var i = 1; i < lines.length; i++) { | |
var obj = {}; | |
var currentline = lines[i].split(","); | |
for (var j = 0; j < headers.length; j++) { | |
if (currentline[j] === "" || currentline[j] === undefined) | |
continue; | |
obj[headers[j]] = currentline[j]; | |
} | |
if (isEmpty(obj)) continue; | |
result.push(obj); | |
} | |
return JSON.stringify(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment