I have a group of JSON arrays that look like this:
[
{
team: 111,
enemyId: 123123,
enemyTeam: '',
winnerId: 7969,
won: 1,
result: '',
dateunix: 1590462000000,
},
{
team: 111,
enemyId: 123123,
enemyTeam: '',
winnerId: 7969,
won: 1,
result: '',
dateunix: 1590462000000,
},
]
They are all in different files, so I have a list and I loop through the files and add their contents into a single variable. Then once all teams have been added, I want to write it into a CSV file:
// Save results in csv
let rawTopTeams = fs.readFileSync('topTeams.json');
let topTeams = JSON.parse(rawTopTeams);
let totalTeams = topTeams.teams.length;
let allResults = [];
topTeams.teams.forEach(function(value){
teamId = value.id;
let resultsJson = fs.readFileSync('team '+ teamId +'.json');
let resultsParsed = JSON.parse(resultsJson);
allResults.push(resultsParsed);
itemsProcessed++
//Checks if all the teams have been added
if (totalTeams <= itemsProcessed) {
const csvWriter = createCsvWriter({
path: 'allResults.csv',
header: [
{id: 'team', title: 'Team 1 ID'},
{id: 'enemyId', title: 'Team 2 ID'},
{id: 'enemyTeam', title: 'Team 2 Name'},
{id: 'won', title: 'Team 1 win?'},
{id: 'winnerId', title: 'Winner ID'},
{id: 'result', title: 'Result'},
{id: 'dateunix', title: 'Date Unix'},
]
});
csvWriter.writeRecords(allResults).then(()=> console.log('The CSV file was written successfully'));
}
})
My issue is I don't know how to call the elements inside the array, instead of the array itself. Right now, when I'm trying to call the csvWriter, the element allResults doesn't actually have the keys I'm calling for the header (mapStatsId, team, enemyId, etc.). Instead, I get a file like this:
Team 1 ID,Team 2 ID,Team 2 Name,Team 1 win?,Winner ID,Result,Date Unix
,,,,function map() { [native code] },,,,,
,,,,function map() { [native code] },,,,,
,,,,function map() { [native code] },,,,,
,,,,function map() { [native code] },,,,,
,,,,function map() { [native code] },,,,,
Can anyone help me fix this?