有以下格式的 json:
[
{
id: 1,
created: "2016-05-04 15:12:40",
content: '...'
},
{
id: 2,
created: "2016-02-04 15:12:40",
content: '...'
},
{
id: 3,
created: "2015-12-04 15:12:40",
content: '...'
},
]
使用 Lodash 的 groupBy 函数 return created 的月份将会得到以下结果。。
{
5: [{
id: 1,
created: "2016-05-04 15:12:40",
content: '...'
}],
2: [{
id: 2,
created: "2016-02-04 15:12:40",
content: '...'
}],
5: [{
id: 3,
created: "2015-12-04 15:12:40",
content: '...'
}],
}
如何处理得到以下结果
[
{
month: '本月',
data: [
{
id: 1,
created: "2016-05-04 15:12:40",
content: '...'
}
]
},
{
month: '4月',
...
}
]
const groupedByMonth = groupBy(this.incomes, item =>
item.updated_at.substring(0, 7)
);
const result = reduce(groupedByMonth, (res, data, date) => {
res.push({ date, data });
return res;
}, []);
return result;
PHP中文网2017-04-11 10:30:20
var re=_.groupBy(arr,function(item){
return (new Date(item.created).getMonth());
})