Skip to content

Instantly share code, notes, and snippets.

@antronic
Last active July 18, 2019 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antronic/ef42ffc6a7ef49aa8b30e96164c63f24 to your computer and use it in GitHub Desktop.
Save antronic/ef42ffc6a7ef49aa8b30e96164c63f24 to your computer and use it in GitHub Desktop.
bh_invoice_cal_ex.js
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