I have managed to write a script to export data from a GET request in Javascript to CSV. However I have a unit variable that has more fields (time, altitude, latitude, o3, co2, ch2o). How can I create a CSV with more columns with the values taken from the script?
So far I have managed to create a CSV with multiple headers, but I don't know how to populate the rows of the CSV with data. I have managed to fully populate only one column.
In the below function, I have used unit[Object.keys(unit)[4]] to only get co2 data. How can I append data to every column?
function download_csv(data, sensor) {
var csv = 'Day, altitude, latitude, o3, co2, ch2o\n';
for (var index in data) {
if (!data.hasOwnProperty(index)) continue;
var unit = data[index];
csv += unit[Object.keys(unit)[4]];
csv += "\n";
}
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(csv);
hiddenElement.target = '_blank';
hiddenElement.download = sensor + '.csv';
hiddenElement.click();
}

data?