Stupid Question on Reducing Results - Operator '+' cannot be applied

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:

function getPlayerStats(player){

    output.text(player);
    let myGames = query.records.filter(record=>record.getCellValueAsString("Player")==player)
    output.inspect(myGames);
    let total_games=output.text(myGames.length);
    let total_wins = myGames.reduce((total,record)=>total + record.getCellValue("Win or Loss"));
    output.text(total_games);
    output.text(total_wins);

}

The output for total_wins ends up being something like this:
[object Object]011
when it really should be 3 - it is concatenating the 1s but not summing them, and missing the first one for some reason. I am new to Javascript and so I figure I am missing something obvious. The warning message is

“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();

console.log(results.records);

let total_wins = results.records.reduce( ( sum, record ) => sum + record.getCellValue('Score'), 0);
console.log(total_wins);

notice the “0” initial value at the end of the reduce function.

JB

2 Likes

That was it! Thanks so much for the assist.