I am trying to summarize a JSON,I have tried several example from Stack Overflow How to get max of in array of objects How to make a foreach of an object in javascript? but none of the tries have been successful.
Sale: [
{
saleID: "2",
timeStamp: "2021-01-08T17:50:28+00:00",
discountPercent: "0",
completed: "true",
archived: "false",
voided: "false",
enablePromotions: "true",
isTaxInclusive: "true",
createTime: "2021-01-08T17:49:53+00:00",
updateTime: "2021-01-08T17:50:29+00:00",
completeTime: "2021-01-08T17:50:28+00:00",
referenceNumber: "",
referenceNumberSource: "",
tax1Rate: "0.2",
tax2Rate: "0",
change: "0",
receiptPreference: "printed",
displayableSubtotal: "500",
ticketNumber: "220000000002",
calcDiscount: "0",
calcTotal: "500",
calcSubtotal: "416.67",
calcTaxable: "416.67",
calcNonTaxable: "0",
calcAvgCost: "0",
calcFIFOCost: "0",
calcTax1: "83.33",
calcTax2: "0",
calcPayments: "500",
total: "500",
totalDue: "500",
displayableTotal: "500",
balance: "0",
customerID: "0",
discountID: "0",
employeeID: "1",
quoteID: "0",
registerID: "1",
shipToID: "0",
shopID: "1",
taxCategoryID: "1",
taxTotal: "83.33"
},
{
saleID: "3",
timeStamp: "2021-01-08T17:53:18+00:00",
discountPercent: "0",
completed: "false",
archived: "false",
voided: "false",
enablePromotions: "true",
isTaxInclusive: "true",
createTime: "2021-01-08T17:53:18+00:00",
updateTime: "2021-01-08T17:53:18+00:00",
referenceNumber: "",
referenceNumberSource: "",
tax1Rate: "0.2",
tax2Rate: "0",
change: "0",
receiptPreference: "printed",
displayableSubtotal: "0",
ticketNumber: "220000000003",
calcDiscount: "0",
calcTotal: "0",
calcSubtotal: "0",
calcTaxable: "0",
calcNonTaxable: "0",
calcAvgCost: "0",
calcFIFOCost: "0",
calcTax1: "0",
calcTax2: "0",
calcPayments: "0",
total: "0",
totalDue: "0",
displayableTotal: "0",
balance: "0",
customerID: "0",
discountID: "0",
employeeID: "1",
quoteID: "0",
registerID: "1",
shipToID: "0",
shopID: "1",
taxCategoryID: "1",
taxTotal: "0"
},
{
saleID: "5",
timeStamp: "2021-01-08T17:54:43+00:00",
discountPercent: "0",
completed: "false",
archived: "false",
voided: "false",
enablePromotions: "true",
isTaxInclusive: "true",
createTime: "2021-01-08T17:54:43+00:00",
updateTime: "2021-01-08T17:54:43+00:00",
referenceNumber: "",
referenceNumberSource: "",
tax1Rate: "0.2",
tax2Rate: "0",
change: "0",
receiptPreference: "printed",
displayableSubtotal: "0",
ticketNumber: "220000000005",
calcDiscount: "0",
calcTotal: "0",
calcSubtotal: "0",
calcTaxable: "0",
calcNonTaxable: "0",
calcAvgCost: "0",
calcFIFOCost: "0",
calcTax1: "0",
calcTax2: "0",
calcPayments: "0",
total: "0",
totalDue: "0",
displayableTotal: "0",
balance: "0",
customerID: "0",
discountID: "0",
employeeID: "1",
quoteID: "0",
registerID: "1",
shipToID: "0",
shopID: "1",
taxCategoryID: "1",
taxTotal: "0"
},
{
saleID: "6",
timeStamp: "2021-01-24T18:49:27+00:00",
discountPercent: "0",
completed: "true",
archived: "false",
voided: "false",
enablePromotions: "true",
isTaxInclusive: "true",
createTime: "2021-01-24T18:48:30+00:00",
updateTime: "2021-01-24T18:49:28+00:00",
completeTime: "2021-01-24T18:49:27+00:00",
referenceNumber: "",
referenceNumberSource: "",
tax1Rate: "0.2",
tax2Rate: "0",
change: "0",
receiptPreference: "printed",
displayableSubtotal: "316.69",
ticketNumber: "220000000006",
calcDiscount: "0",
calcTotal: "316.69",
calcSubtotal: "263.91",
calcTaxable: "263.91",
calcNonTaxable: "0",
calcAvgCost: "0",
calcFIFOCost: "0",
calcTax1: "52.78",
calcTax2: "0",
calcPayments: "316.69",
total: "316.69",
totalDue: "316.69",
displayableTotal: "316.69",
balance: "0",
customerID: "0",
discountID: "0",
employeeID: "1",
quoteID: "0",
registerID: "1",
shipToID: "0",
shopID: "1",
taxCategoryID: "1",
taxTotal: "52.78"
}]
I have tried several ways, the latest one:
const debitSummary = sales.reduce((acc, inv, i) => {
// console.log("inside reduce");
if (i) {
// if (parseFloat(inv.calcTotal>0)){ // invoice
acc = {
//...acc,
ticketNumber: acc.ticketNumber + ',' + inv.ticketNumber,
registerID: inv.registerID > acc.registerID ? inv.registerID : acc.registerID,
calcTotal: parseFloat(acc.calcTotal) + parseFloat(inv.calcTotal),
timeStamp: formatDate(inv.timeStamp) > formatDate(acc.timeStamp) ? formatDate(inv.timeStamp) : formatDate(acc.timeStamp),
}
// }
}
return acc;
},sales[0] );
But impossible to achieve the goal:
- sum of calcTotal,
- sum of total,
- array of saleID,
- array of ticketNumber,
- with a break by registerID and day of timeStamp:
[{
calcTotal: "500",
registerID: "1",
saleID: {
"2",
"3",
"5"
},
ticketNumber: {
"220000000002",
"220000000003",
"220000000005"
},
timeStamp: "2021-01-08",
total: "500"
},
{
calcTotal: "316.69",
registerID: "1",
saleID: "6",
ticketNumber: {
"220000000006"
},
timeStamp: "2021-01-24",
total: "316.69"
}
]