在每年的每个月为js组加下划线 [英] Underscore js group by each month on each year

查看:13
本文介绍了在每年的每个月为js组加下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的输入json

"data": [
        {
            "id":3,
            "created_by": 1,
            "created_at": "2022-01-31T07:00:01.880Z",
        },
        {
            "id":2,
            "created_by": 1,
            "created_at": "2022-01-31T07:00:01.880Z",
        },
        {
            "id":1,
            "created_by": 1,
            "created_at": "2022-01-31T07:00:01.880Z",
        }
    ]
我想用年和月对结果json输出进行排序,这意味着每年它必须按每个月分组,因为我尝试了下面的代码,它对年和月都可以很好地工作,但我不确定如何将两者组合在一起我尝试在grouedByMonth内调用grouedBy Year,但它没有给我输出,而是抛出了未定义的PUT代码,输出如下所示。也是我一直在寻找的结果。

    var groupedByYear = _.groupBy(flashMessage,function(item:any) {
        return item.created_at.getFullYear();
    });

    var groupedByMonth = _.groupBy(flashMessage,function(item:any) {
        return item.created_at.getMonth();
    });

两者都正确返回结果。具体如下。 1)

 "2021": [
            {
                "created_by": 1,
                "created_at": "2021-01-31T06:54:27.733Z",
            }
        ],
       "2022": [
            {
                "created_by": 1,
                "created_at": "2022-01-31T06:54:27.733Z",
            }
        ],
  1. "0": [
             {
                 "created_by": 1,
                 "created_at": "2021-01-31T06:54:27.733Z",
             }
         ],
        "1": [
             {
                 "created_by": 1,
                 "created_at": "2022-02-31T06:54:27.733Z",
             }
         ],
    

但我想要这样的结果

   "2021": [
               0:[ {
                    "created_by": 1,
                    "created_at": "2021-01-31T06:54:27.733Z",
                }]
            ],
           "2022": [
               0:[ {
                    "created_by": 1,
                    "created_at": "2022-01-31T06:54:27.733Z",
                }]
               1:[ {
                    "created_by": 1,
                    "created_at": "2022-02-31T06:54:27.733Z",
                }]

            ],

如何才能做到这一点?我在NodeJS中使用类型脚本,我的数据库是postgres。有没有人能帮我这个忙?

推荐答案

您可以使用简单的Reduce键而不加下划线

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
const flashMessages = [
            {
             "created_by": 1,
             "created_at": "2021-01-31T06:54:27.733Z",
             },
           {
             "created_by": 2,
             "created_at": "2021-02-25T06:54:27.733Z",
             },
            {
             "created_by": 3,
             "created_at": "2021-01-31T06:54:27.733Z",
             },
          {
         "created_by": 3,
         "created_at": "2022-02-01T06:54:27.733Z",
         },
         
         ]
         
         const months = [
           'jan',
           'feb',
           'mar',
           'apr',
           'may',
           'jun',
           'jul',
           'sep',
           'oct',
           'nov',
           'dec'
         ]
         
const result = flashMessages.reduce((res, item) => {
   const date = new Date(item.created_at)
   
   const year = date.getFullYear();
   const month = months[date.getMonth()]
   
   const existingYear = res[year] || {}
   res[year] = existingYear
   const updated  = [...(existingYear[month] || []), item]
   
   res[year][month] = updated
   
   return res
}, {});

console.log(result)

这篇关于在每年的每个月为js组加下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆