I am trying to summarize a JSON
var data = [
{
customerName: "Customer1",
customerId: "1234",
invoices: [
{
id: "647549",
transactionId: "INV01",
date: "10/12/2020",
debit: 371.93,
dueDate: "09/02/2021"
}
],
creditmemo: []
},
{
customerName: "Customer5",
customerId: "5678",
invoices: [
{
id: "631109",
transactionId: "INV05",
date: "09/12/2020",
debit: 206.92,
dueDate: "08/02/2021"
},
{
id: "664359",
transactionId: "INV06",
date: "11/12/2020",
debit: 91.91,
dueDate: "10/02/2021"
}
],
creditmemo: []
}
];
I have tried several ways, the latest one:
//console.log(data.length);
var data2 = [];
for (let value in data) {
console.log(data[value].customerName);
//console.log(data[value]);
var invoices = [];
invoices.push(data[value].invoices[0]);
//console.log(1);
//console.log(customer);
/* var max = new Date(
Math.max.apply(
null,
customer.map(function (o) {
var dateString = o.dueDate;
var dateParts = dateString.split("/");
var dateObject = new Date(
+dateParts[2],
dateParts[1] - 1,
+dateParts[0]
);
return new Date(dateObject);
})
)
);*/
var result = [
invoices.reduce((acc, n) => {
for (var prop in n) {
console.log("prop: " + prop);
if (prop === "debit") {
if (acc[prop]) {
acc[prop] += n[prop];
console.log("OK");
} else {
acc[prop] = n[prop];
}
} else if (prop === "dueDate") {
//acc[prop] = max;
} else {
acc[prop] = n[prop];
}
}
return acc;
}, {})
];
data2.push(result);
}
console.log(data2);
I would like to have the following result, sum all the debit of the invoices per customer, get the max date per customer (I didn't find a way to remove the empty property creditmemo
[
{
customerName: "Customer1",
customerId: "1234",
invoices: [
{
id: "647549",
transactionId: "INV01",
date: "10/12/2020",
debit: 371.93,
dueDate: "09/02/2021"
}
]
},
{
customerName: "Customer5",
customerId: "5678",
invoices: [
{
id: "631109-664359",
transactionId: "INV05-INV06",
date: "11/12/2020",
debit: 298.83,
dueDate: "08/02/2021"
}
}
];