The below code which I need to use, with this Key: value data array does NOT work. Produces an error on this line:
+ data.map(e => e.join(",")).join("\n");
const downloadButton = document.getElementById("downloadButton")
let data = [{
title: "",
url: "",
name: "",
company: "",
email: ""
}]
let csvContent = "data:text/csv;charset=utf-8,"
+ data.map(e => e.join(",")).join("\n");
var encodedUri = encodeURI(csvContent);
window.open(encodedUri);
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "my_data.csv");
document.body.appendChild(link);
downloadButton.addEventListener("click", function() {
link.click()
})
BUT this below code works, with this type data array
const downloadButton = document.getElementById("downloadButton")
let data = [
["rahul", "delhi", "accounts dept"],
["rajeev", "UP", "sales dept"]
];
let csvContent = "data:text/csv;charset=utf-8,"
+ data.map(e => e.join(",")).join("\n");
var encodedUri = encodeURI(csvContent);
window.open(encodedUri);
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "my_data.csv");
document.body.appendChild(link);
downloadButton.addEventListener("click", function() {
link.click()
})
Only difference is the type of data array. First one has Key: value pair. How does one get this to download to a simple .CSV file?