I would like to export a javascript array of objects into excel or even a database, something similar to https://jsfiddle.net/3an24jmw/25/
I tried to use the jsfiddle.net code above, and am getting a console error:
Uncaught TypeError: RowItem.forEach is not a function at Array.forEach () at exportToCsv at HTMLButtonElement.onclick
My array and javascript looks something like this:
var items = [
{
title: 'title1',
youtube: 'youtubevideoid',
audioaddress: "audio1.mp3",
year: '2022',
month: '07',
day: '22',
time: 'AM',
speaker: 'speaker1'
},
{
title: 'title2',
youtube: 'youtubevideoid2',
audioaddress: "audio2.mp3",
year: '2022',
month: '07',
day: '22',
time: 'PM',
speaker: 'speaker2'
},
];
//Download CSV of Javascript Array
exportToCsv = function() {
var CsvString = "";
items.forEach(function(RowItem, RowIndex) {
RowItem.forEach(function(ColItem, ColIndex) {
CsvString += ColItem + ',';
});
CsvString += "\r\n";
});
CsvString = "data:application/csv," + encodeURIComponent(CsvString);
var x = document.createElement("A");
x.setAttribute("href", CsvString );
x.setAttribute("download","items.csv");
document.body.appendChild(x);
x.click();
}
And the html button code looks like this:
<button onclick="exportToCsv()">export to CSV</button>