Suppose I have the following structure:
var obj = [{one: 1, two: 2, three: 3}, {one: 1, two: 2, three: 3}];
But I need to export this data to csv with the following output:
"1", "2", "3"
"1", "2", "3"
I've tried the following code, but it does not work:
var csvContent = "data:text/csv;charset=utf-8,";
Object.values = function (obj) {
var vals = [];
for( var key in obj ) {
if ( obj.hasOwnProperty(key) ) {
vals.push(obj[key]);
}
}
return vals;
}
Object.values(obj).forEach(function(infoArray, index) {
dataString = infoArray.join(",");
csvContent += index < obj.length ? dataString + "\n" : dataString;
});
var encodedUri = encodeURI(prepearedString);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "my_data.csv");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
Could you please help me with this issue.
encodeURI(prepearedString)? MaybeencodeURI(csvContent)works better?