JS 根据数组对象中某个字段的值,重组新的数组对象

1 篇文章 0 订阅

我现在有一个数组对象为:

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)

结果:在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值