Skip to content

Instantly share code, notes, and snippets.

@techb
Created March 14, 2019 16:58
Show Gist options
  • Save techb/1621c4fae80916b39cd5679abab085fc to your computer and use it in GitHub Desktop.
Save techb/1621c4fae80916b39cd5679abab085fc to your computer and use it in GitHub Desktop.
JS Array to CSV Download
var csv_arr = [];
// add header first before pushing data
csv.push(["Bruh", "Data", "Date", "Ect"]);
for(var i = 0; i <= somedata.length; i++){
csv_arr.push(somedata[i]);
}
// found: https://gist.github.com/yangshun/01b81762e8d30f0d7f8f
document.querySelector("#muh_epic_button").addEventListener("click", function () {
var csvString = csv_arr.join('\r\n'); // Windows newline
var a = document.createElement('a');
a.href = 'data:attachment/csv,' + csvString;
a.target = '_blank';
a.download = 'data.csv';
document.body.appendChild(a);
a.click();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment