慕的地6264312
2021-11-25 15:35:43
這是我的 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 回答
MMTTMM
TA貢獻1869條經驗 獲得超4個贊
好的,對于您的具體示例,以下內容會產生您想要的結果:
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
提交
取消
