This is how my json looks like:
invoices = { "count": 1 "data": Array(0)[{ "company": "ABC PLC", "customer": { "name": "XYZ" }, "invoiceFees": Array(2)[ 0: { "quarter": { "q_name": "A", "description": "payments for quarter A" } }, 1: { "quarter": { "q_name": "B", "description": "payments for quarter B" } } ] }] }
I am trying to group invoice results by Quarter. This is how I am doing it:
console.log(_.groupBy(invoices.data.invoiceFees, 'quarter.q_name'));
This slices everything down to an empty result like this:
invoices = {}
What am I doing wrong here?
My desired output sould be something of this sort:
invoices = { "data": [{ "company": "ABC PLC", "customer": { "name": "XYZ" }, "invoiceFees": [{ "quarter A: { "description": "payments for quarter A" }, "quarter B: { "description": "payments for quarter B" } }] }] }
Ok, for your specific example, the following produces the result you're after:
invoices = {
data: _.map(invoices.data, d => _.assign(
_.omit(d, 'invoiceFees'),
_.fromPairs(
_.map(d.invoiceFees, f => [
'quarter ' + f.quarter.q_name,
_.omit(f.quarter, 'q_name')
])
)
))
}
if you need invoiceFees
as a separate object, then
invoices = {
data: _.map(invoices.data, d => _.assign(
d,
{invoiceFees: _.fromPairs(
_.map(d.invoiceFees, f => [
'quarter ' + f.quarter.q_name,
_.omit(f.quarter, 'q_name')
])
)}
))
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.