每年每个月下划线js组

Underscore js group by each month on each year

这是我的输入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 进行排序,这意味着每年它必须按每个月分组,为此我尝试了下面的代码,它仅在年和月中都可以正常工作但是我不确定如何将两者结合在一起 我尝试在 groupedByMonth 中调用 groupedByYear 但它没有给我输出而是抛出 Undefined 输出代码,输出如下所示。以及我正在寻找的结果。

    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

在没有下划线的情况下做到这一点

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)

您也可以通过添加 _.mapObject 以干净的功能样式获得所需的结果。

var 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",
}];

var months = 'jan feb mar apr may jun jul aug sep oct nov dec'.split(' ');

function getDate(item) {
    return new Date(item.created_at);
}

function getMonth(item) {
    return months[getDate(item).getMonth()];
}

function getYear(item) {
    return getDate(item).getFullYear();
}

function groupByMonths(items) {
    return _.groupBy(items, getMonth);
}

var byYear = _.groupBy(data, getYear);
var result = _.mapObject(byYear, groupByMonths);

console.log(result);
<script src="https://underscorejs.org/underscore-umd-min.js"></script>

对于奖励积分,您也可以使用 _.partial,这样您就不必编写 groupByMonths 函数。在那种情况下,最后一行变成这样:

var result = _.mapObject(byYear, _.partial(_.groupBy, _, getMonth));