I have a column “Win or Loss” that is a 0 or 1. It is the result of a formula in the Table. I would like to filter a result set to a certain number of rows in that table, which is working, and then sum the “Win or Loss” column. I figure this can be done with a reduce function. In this function which I call for each player, the goal is to calculate how many wins they have:
let myGames = query.records.filter(record=>record.getCellValueAsString("Player")==player)
let total_wins = myGames.reduce((total,record)=>total + record.getCellValue("Win or Loss"));
The output for total_wins ends up being something like this: [object Object]011
“Operator ‘+’ cannot be applied to types ‘Games_Players_RolesTable_Record’ and ‘number’.”
which explains why this is wierd, but I can’t figure out the right way to do it. Any pointers would be appreciated. Thank you!
Hi @BRD529 - I think the issue here is that your reduce function is acting upon an array of objects, rather than just an array. From Mozilla:
the reduce function needs an initial value in this case.
A slightly different set-up, but this script works:
let table = base.getTable("Table 1");
let results = await table.selectRecordsAsync();
let total_wins = results.records.reduce( ( sum, record ) => sum + record.getCellValue('Score'), 0);
notice the “0” initial value at the end of the reduce function.