Below is the provided code snippet:
Definition of Interface -
interface IWEXInterface {
readonly Date?: string;
"Exec Qty"?: string;
readonly Expiry?: string;
}
Data Collection -
let data: IWEXInterface[] = [
{
Date: "8/30/2021",
"Exec Qty": "13",
Expiry: "17/09/2021",
},
{
Date: "8/30/2021",
"Exec Qty": "15",
Expiry: "17/09/2021",
},
{
Date: "8/30/2021",
"Exec Qty": "13",
Expiry: "17/09/2021",
},
];
Aggregation and Grouping Process -
const sums = [
...data
.reduce((map, item) => {
const { Date: key, "Exec Qty": qty } = item;
const prev: IWEXInterface = map.get(key);
if (prev) {
prev["Exec Qty"]! += Number(qty);
} else {
map.set(key, Object.assign({}, item));
}
return map;
}, new Map())
.values(),
];
console.log(sums);
The final Output -
[ { Date: '8/30/2021', 'Exec Qty': '131513', Expiry: '17/09/2021' } ]
Query on Converting String to Number.
Desired Outcome -
[ { Date: '8/30/2021', 'Exec Qty': 41, Expiry: '17/09/2021' } ]
.............................................................................................................