Last active
July 18, 2019 13:49
-
-
Save antronic/ef42ffc6a7ef49aa8b30e96164c63f24 to your computer and use it in GitHub Desktop.
bh_invoice_cal_ex.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = { | |
invoices: [ | |
{ | |
chargeDetail: [ | |
{ | |
description: 'ค่ายา', | |
chargeDetailSubWTF: [ | |
{ | |
cost: 10, | |
}, | |
{ | |
cost: 20, | |
} | |
] | |
} | |
] | |
}, | |
{ | |
chargeDetail: [ | |
{ | |
description: 'ค่าหมอนวด', | |
chargeDetailSubWTF: [ | |
{ | |
cost: 3500, | |
}, | |
{ | |
cost: 2500, | |
} | |
] | |
} | |
] | |
}, | |
] | |
} | |
/* | |
data -> {invoices: Array(2)} | |
*/ | |
invoices = {} | |
data.invoices.forEach((item, index) => { | |
invoiceName = 'invoice_' + index | |
thisInvoice = {} | |
item.chargeDetail.forEach((c, i) => { | |
var chargeDetails = 0 | |
c.chargeDetailSubWTF.forEach((d, ii) => { | |
chargeDetails += d.cost | |
}) | |
thisInvoice = { | |
...thisInvoice, | |
[c.description]: { | |
total: chargeDetails, | |
} | |
} | |
}) | |
// save invoice | |
invoices = { | |
...invoices, | |
[invoiceName]: thisInvoice | |
} | |
}) | |
/* | |
invoices -> | |
{ | |
invoice_0: { | |
ค่ายา: {total: 30} | |
} | |
invoice_1: { | |
ค่าหมอนวด: {total: 6000} | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment