【发布时间】:2020-01-17 13:28:38
【问题描述】:
我有一个看起来像这样的对象:
{
data: [
{ id: "1", state: "accepted", estimate_date: "2019-12-17" },
{ id: "2", state: "rejected", estimate_date: "2019-12-17" },
{ id: "3", state: "open", estimate_date: "2019-12-17" },
{ id: "4", state: "open", estimate_date: "2019-12-18" },
{ id: "5", state: "rejected", estimate_date: "2019-12-18" },
{ id: "6", state: "accepted", estimate_date: "2019-12-18" },
]
}
当我在这样的对象上使用 lodash groupBy 时:
const key = 'data';
const groupedEstimates = groupBy(estimateData[key], 'estimate_date');
返回:
[
[
"2019-12-17"
],
[
[ { id: "1", state: "accepted" } ],
[ { id: "2", state: "rejected" } ],
[ { id: "3", state: "open" } ]
]
],
[
[
"2019-12-18"
],
[
[ { id: "4", state: "open" } ],
[ { id: "5", state: "rejected" } ],
[ { id: "6", state: "accepted" } ]
]
]
但现在我正在尝试实现这样的目标:
[
{
date: "2019-12-17",
items: [
{ id: "1", state: "accepted" },
{ id: "2", state: "rejected" },
{ id: "3", state: "open" },
]
},
{
date: "2019-12-18",
items: [
{ id: "4", state: "open" },
{ id: "5", state: "rejected" },
{ id: "6", state: "accepted" },
]
}
]
除非我不知道如何使用 lodash 来实现。它不必使用 lodash,但我只在开始时使用它,因为它似乎是解决我的问题的简单方法。现在我正在尝试实现更合理的数据结构,我想了解如何实现它。
【问题讨论】:
标签: javascript arrays data-structures lodash javascript-objects