Skip to content

Instantly share code, notes, and snippets.

@elsheikh21
Created January 15, 2018 16:57
Show Gist options
  • Save elsheikh21/0c559c6e2fcd8b8981ad1f2a48b49d6e to your computer and use it in GitHub Desktop.
Save elsheikh21/0c559c6e2fcd8b8981ad1f2a48b49d6e to your computer and use it in GitHub Desktop.
Parsing CSV file client side, sending json object to Server
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