I've successfully implemented grouping by using the reduce function in typescript with my javascript. However, I'm struggling to figure out how to sum another property.
Currently, everything is grouped by the property: pacct (which is working).
The issue arises when trying to sum the property: quantity.
If anyone could provide assistance, it would be greatly appreciated.
Below is the export interface from another .ts file:
export interface TradeData {
id: number;
filedate: Date;
poffic: string;
pacct: string;
quantity: number;
sector: string;
psdsC1: string;
name: string;
bbsymbol: string;
last_price: number;
deltasettlement: number;
}
Here is the code snippet where I use reduce:
const result = trades.reduce((groupedAccounts, trade) => {
const account = trade.pacct;
if (groupedAccounts[account] == null) groupedAccounts[account] = [];
groupedAccounts[account].push(trade);
return groupedAccounts;
}, {} as Record<string, TradeData[]>);