我有以下firebase数据库。
{
"orders" : [ null, {
"date" : "2018-07-09 10:07:18",
"item" : "apple",
"price" : 30 },
{
"date" : "2018-06-09 10:07:18",
"item" : "bread",
"price" : 25
},
{
"date" : "2018-06-13 10:07:18",
"item" : "milk",
"price" : 25
},
{
"date" : "2018-07-05 10:07:18",
"item" : "cheese",
"price" : 50
}
]
}
我想在Vue项目中检索数据,但是我不知道如何。在不同的时间有订单,我想使用groupByChild('date')方法,但是由于日期是唯一的,因此它返回4个对象。我想在订单日期的月份中对订单进行分组。我如何在7-8之间将对象分组。漫画家?
答案 0 :(得分:1)
您可以将ISO日期,年份和月份的一部分用于分组。
var data = { orders: [null, { date: "2018-07-09 10:07:18", item: "apple", price: 30 }, { date: "2018-06-09 10:07:18", item: "bread", price: 25 }, { date: "2018-06-13 10:07:18", item: "milk", price: 25 }, { date: "2018-07-05 10:07:18", item: "cheese", price: 50 }] },
groups = data.orders.reduce((r, o) => {
var key;
if (!o) {
return r;
}
key = o.date.slice(0, 7);
r[key] = r[key] || [];
r[key].push(o);
return r;
}, Object.create(null));
console.log(groups);
.as-console-wrapper { max-height: 100% !important; top: 0; }