Having trouble calculating the total sum of bills in an order list using typescript. The error message 'Object is of type 'unknown' keeps popping up when trying to access the total bill field. Interestingly, it works fine in the browser console but throws an error in Visual Studio Code.
Below is the JSON response:
{
"order_list": {
"32212000000037": {
"invoice_id": "32212000000037",
"customer_id": "006019000015",
"customer_name": "Spantik",
"inv_date": "2020-12-24",
"prod_date": "2020-12-24",
"inv_time": "09:00",
"total_bill": 22,
"notification": "waiting",
"sales_id": "",
"inv_type": "regular",
"danger": "over"
},
"32212000000036": {
"invoice_id": "32212000000036",
"customer_id": "006019000015",
"customer_name": "Spantik",
"inv_date": "2020-12-24",
"prod_date": "2020-12-24",
"inv_time": "14:57",
"total_bill": 22,
"notification": "waiting",
"sales_id": "",
"inv_type": "regular",
"danger": "over"
}
}
}
Here's my attempt at solving the issue:
countTotalBill(orderDate: string) {
let totalBill = 0;
const itemList = this.orderLists[orderDate];
if (itemList != null) {
const arr = Object.values(itemList);
arr.filter(ele => {
totalBill = totalBill + ele.total_bill; // <<<<<<======= here is the problem
});
}
return totalBill;
}