这是我的 json 的样子: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" } } ] }]}我正在尝试按季度对发票结果进行分组。这就是我的做法:console.log(_.groupBy(invoices.data.invoiceFees, 'quarter.q_name'));这会将所有内容切成空结果,如下所示:invoices = {}我在这里做错了什么?我想要的输出应该是这样的:invoices = {"data": [{ "company": "ABC PLC", "customer": { "name": "XYZ" }, "invoiceFees": [{ "quarter A: { "description": "payments for quarter A" }, "quarter B: { "description": "payments for quarter B" }}]}]}
1 回答
长风秋雁
TA贡献1757条经验 获得超7个赞
好的,对于您的具体示例,以下内容会产生您想要的结果:
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')
])
)
))
}
如果你需要invoiceFees作为一个单独的对象,那么
invoices = {
data: _.map(invoices.data, d => _.assign(
d,
{invoiceFees: _.fromPairs(
_.map(d.invoiceFees, f => [
'quarter ' + f.quarter.q_name,
_.omit(f.quarter, 'q_name')
])
)}
))
}
添加回答
举报
0/150
提交
取消