Created
November 1, 2018 10:02
-
-
Save mrsid96/9455ad84d2e0c824af06abc7b7666202 to your computer and use it in GitHub Desktop.
Create a csv file from js array from frontend
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
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