This function converts a Javascript Object into a CSV.
var dataFromAPI = [{"name":"first"},{"name":"sec,ond"},{"name":"third 'jibberish"},{"name":"fourth, jibberish"}]
function convertToCSVandDownload(objArray) {
var csvOutput = "";
Object.keys(objArray).forEach(function(key) {
if (csvOutput.length == 0) {
csvOutput = "Index,Field Value\n";
csvOutput = csvOutput + JSON.stringify(key) + "," + JSON.stringify(objArray[key]["name"]) + "\n";
} else {
csvOutput = csvOutput + JSON.stringify(key) + "," + JSON.stringify(objArray[key]["name"]) + "\n";
}
})
return csvOutput;
}
console.log(convertToCSVandDownload(dataFromAPI));