let list = [
{id: 564, gmt_create: "2021-06-11 17:46:46", month: "2021-06", type: 1, change_amount: 0.1},
{id: 555, gmt_create: "2021-06-10 14:40:23", month: "2021-06", type: 1, change_amount: 0.006},
{id: 553, gmt_create: "2021-07-10 12:02:01", month: "2021-07", type: 1, change_amount: 0.006},
{id: 551, gmt_create: "2021-07-10 11:55:01", month: "2021-07", type: 1, change_amount: 0.006},
{id: 549, gmt_create: "2021-08-10 11:39:01", month: "2021-08", type: 1, change_amount: 0.006},
{id: 547, gmt_create: "2021-08-10 11:19:01", month: "2021-08", type: 1, change_amount: 0.006},
{id: 545, gmt_create: "2021-10-10 11:11:39", month: "2021-10", type: 1, change_amount: 0.006},
{id: 543, gmt_create: "2021-11-10 11:07:46", month: "2021-11", type: 1, change_amount: 0.006},
{id: 541, gmt_create: "2021-11-10 11:04:01", month: "2021-11", type: 1, change_amount: 0.006},
{id: 539, gmt_create: "2021-11-10 11:00:11", month: "2021-11", type: 1, change_amount: 0.006}
]
希望变成下面的
[
{
'month':'2021-06',
'data':[
{id: 564, gmt_create: "2021-06-11 17:46:46", month: "2021-06", type: 1, change_amount: 0.1},
{id: 555, gmt_create: "2021-06-10 14:40:23", month: "2021-06", type: 1, change_amount: 0.006}
]},
{
'month':'2021-07',
'data':[
{id: 553, gmt_create: "2021-07-10 12:02:01", month: "2021-07", type: 1, change_amount: 0.006},
{id: 551, gmt_create: "2021-07-10 11:55:01", month: "2021-07", type: 1, change_amount: 0.006}
]},
{
'month':'2021-08',
'data':[
{id: 549, gmt_create: "2021-08-10 11:39:01", month: "2021-08", type: 1, change_amount: 0.006},
{id: 547, gmt_create: "2021-08-10 11:19:01", month: "2021-08", type: 1, change_amount: 0.006}
]},
{
'month':'2021-10',
'data':[
{id: 545, gmt_create: "2021-10-10 11:11:39", month: "2021-10", type: 1, change_amount: 0.006}
]},
{
'month':'2021-11',
'data':[
{id: 543, gmt_create: "2021-11-10 11:07:46", month: "2021-11", type: 1, change_amount: 0.006},
{id: 541, gmt_create: "2021-11-10 11:04:01", month: "2021-11", type: 1, change_amount: 0.006},
{id: 539, gmt_create: "2021-11-10 11:00:11", month: "2021-11", type: 1, change_amount: 0.006}
]}
]
处理过程:
let list = [
{id: 564, gmt_create: "2021-06-11 17:46:46", month: "2021-06", type: 1, change_amount: 0.1},
{id: 555, gmt_create: "2021-06-10 14:40:23", month: "2021-06", type: 1, change_amount: 0.006},
{id: 553, gmt_create: "2021-07-10 12:02:01", month: "2021-07", type: 1, change_amount: 0.006},
{id: 551, gmt_create: "2021-07-10 11:55:01", month: "2021-07", type: 1, change_amount: 0.006},
{id: 549, gmt_create: "2021-08-10 11:39:01", month: "2021-08", type: 1, change_amount: 0.006},
{id: 547, gmt_create: "2021-08-10 11:19:01", month: "2021-08", type: 1, change_amount: 0.006},
{id: 545, gmt_create: "2021-10-10 11:11:39", month: "2021-10", type: 1, change_amount: 0.006},
{id: 543, gmt_create: "2021-11-10 11:07:46", month: "2021-11", type: 1, change_amount: 0.006},
{id: 541, gmt_create: "2021-11-10 11:04:01", month: "2021-11", type: 1, change_amount: 0.006},
{id: 539, gmt_create: "2021-11-10 11:00:11", month: "2021-11", type: 1, change_amount: 0.006}
]
let monthes = list.map(item => {
return {month:item.month,data:[]}
})
//数组去重
let hash = {};
monthes = monthes.reduce((item, next) => {
hash[next.month] ? '' : hash[next.month] = true && item.push(next);
return item
}, []);
//给每个对象的data追加数据
list.forEach(item => {
for(let i in monthes) {
if(item.month == monthes[i].month) {
monthes[i].data.push(item);
}
}
})
console.log(monthes)