Skip to content

Instantly share code, notes, and snippets.

@mrsid96
Created November 1, 2018 10:02
Show Gist options
  • Save mrsid96/9455ad84d2e0c824af06abc7b7666202 to your computer and use it in GitHub Desktop.
Save mrsid96/9455ad84d2e0c824af06abc7b7666202 to your computer and use it in GitHub Desktop.
Create a csv file from js array from frontend
var rows = [["name1", "city1", "some other info"], ["name2", "city2", "more info"]];
var csvContent = "data:text/csv;charset=utf-8,";
rows.forEach(function(rowArray){
let row = rowArray.join(",");
csvContent += row + "\r\n";
});
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "my_data.csv");
document.body.appendChild(link); // Required for FF
link.click(); // This will download the data file named "my_data.csv".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment