我正在尝试总结一个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: []
}
];
我尝试了几种方法,最新的一种:
//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);
我想得到以下结果,求和每个客户的所有发票的总和,获得每个客户的最大日期(我没有找到一种方法来删除空资产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"
}
}
];
这是map()
数据数组相当简单的方法,它带有嵌套reduce()
调用以重构发票数组。invoices
数组中的第一个元素作为初始累加器传递,并且只有在数组中有多个元素时才进行重构。由于我们在每次地图迭代时都返回一个新对象,因此我们可以简单地忽略该creditMemo
属性。
我同意评论员的意见,即id
在数组中存储多个属性,而不是在字符串中存储多个属性,以便以后使用和简化重构。
const summary = data.map(({ customerName, customerId, invoices }) => {
const debitSummary = invoices.reduce((acc, inv, i) => {
if (i) {
acc = {
...acc,
id: acc.id + '-' + inv.id,
transactionId: acc.transactionId + '-' + inv.transactionId,
debit: acc.debit + inv.debit,
date: new Date(inv.date) > new Date(acc.date) ? inv.date : acc.date,
}
}
return acc;
}, invoices[0]);
return { customerName, customerId, invoices: [{...debitSummary}] };
});
console.log(summary);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script>
const 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: [] }];
</script>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句