-
-
Save ovicko/ab3d54adb31b61f62e47de343d09f1ff to your computer and use it in GitHub Desktop.
Converts a 2D array into a CSV file
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 arrayToCSV (twoDiArray) { | |
// Modified from: http://stackoverflow.com/questions/17836273/ | |
// export-javascript-data-to-csv-file-without-server-interaction | |
var csvRows = []; | |
for (var i = 0; i < twoDiArray.length; ++i) { | |
for (var j = 0; j < twoDiArray[i].length; ++j) { | |
twoDiArray[i][j] = '\"' + twoDiArray[i][j] + '\"'; // Handle elements that contain commas | |
} | |
csvRows.push(twoDiArray[i].join(',')); | |
} | |
var csvString = csvRows.join('\r\n'); | |
var a = document.createElement('a'); | |
a.href = 'data:attachment/csv,' + csvString; | |
a.target = '_blank'; | |
a.download = 'myFile.csv'; | |
document.body.appendChild(a); | |
a.click(); | |
// Optional: Remove <a> from <body> after done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment