Javascript 使用lodash groupBy以正确的方式构造数组
我有一个像这样的对象:Javascript 使用lodash groupBy以正确的方式构造数组,javascript,arrays,data-structures,lodash,javascript-objects,Javascript,Arrays,Data Structures,Lodash,Javascript Objects,我有一个像这样的对象: { 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", st
{
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" },
]
}
const key = 'data';
const groupedEstimates = groupBy(estimateData[key], 'estimate_date');
[
{
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 groupBy时,如下所示:
{
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" },
]
}
const key = 'data';
const groupedEstimates = groupBy(estimateData[key], 'estimate_date');
[
{
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" },
]
}
]
它返回:
[
[
"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" } ]
]
]
但现在我正试图实现这样的目标:
{
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" },
]
}
const key = 'data';
const groupedEstimates = groupBy(estimateData[key], 'estimate_date');
[
{
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,但我只是在一开始使用它,因为它似乎是解决我问题的简单方法。现在,我正在尝试实现一个更合理的数据结构,我希望了解如何实现它。在通过
估计\u日期
属性进行分组后,使用.map()
迭代groups对象。通过获取日期
属性的键(第二个参数),并映射项
以忽略估计日期
,生成组的对象:
const estimateData={“data”:[{“id”:“1”,“state”:“已接受”,“估计日期”:“2019-12-17”},{“id”:“2”,“state”:“拒绝”,“估计日期”:“2019-12-17”},{“id”:“3”,“state”:“open”,“估计日期”:“2019-12-17”{“id”:“4”,“state”:“open”,“估计日期”:“2019-12-18”},{“id”:“5”,“state”:“已拒绝”,“估计日期”:“2019-12-18”{“id”:“6”,“state”:“接受”、“估计日期”:“2019-12-18”}
const groupedEstimates=\映射(
_.groupBy(estimateData.data,“估计日期”),
(项目、日期)=>({
日期,
items:items.map(o=>\uu.omit(o,'estimate\u date'))
})
)
console.log(groupedEstimates)
我不得不将其更改为estimateData['data']
,或者在我的情况下,estimateData[key]
,因为我已经声明了。您的其余答案完全符合我的要求。谢谢。这对解决方案都没有关系。这就是为什么我要更改-以减少混乱。欢迎:)