如何按天、周、月对具有时间戳属性的对象进行分组?

How to group objects with timestamps properties by day, week, month?(如何按天、周、月对具有时间戳属性的对象进行分组?)
本文介绍了如何按天、周、月对具有时间戳属性的对象进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 nodejs 应用程序中,我有一个事件对象数组,格式如下:

In a nodejs application, I have an array of event objects formatted as follows:

eventsArray = [ {id: 1, date: 1387271989749 }, {id:2, date: 1387271989760}, ... ]

eventsArray 具有可变长度的 n 个元素,假设我选择时间参考为巴黎时间,我希望能够按天、周或月对元素进行分组:

eventsArray having a variable length of n elements, and supposing I choose time reference to be Paris time, I want to be able to group elements by day, week, or month:

groupedByDay = {

            2012: { ... },
            2013: {
              dayN  : [{id: a0, date: b0}, {id: a1, date: b1}, {id: a2, date: b2 }],
              dayN+1: [{id: a3, date: b3}, {id: a4, date: b4}, {id: a5, date: b5 }],
              ...
                   },
            2014: { ... }

          }

groupedByWeek = {
            2012: { ... }
            2013: {
              weekN: [{id: a0, date: b0}, {id: a1, date: b1}, {id: a2, date: b2 }],
              weekN+1: [{id: a3, date: b3}],
              ....
                  },
             2014: { ... }
                }

groupedByMonth = {
             2012: { ... },
             2013: {
               monthN: [ {id: a0, date: b0 }, {id: a1, b1}, {id: a2, b2}, {id: a3, b3 }],
               monthN+1: [ {id: a4, date: b4 }, {id: a5, b5}, {id: a6, b6}],
               ...
                   },
              2014: { ... }
                 }

在处理 unix 时间戳方面几乎没有经验,我想知道如何做到这一点,或者是否有一个 npm 模块可以让这更容易.

Having very little experience with manipulating unix timestamps, I was wondering how this could be done or if there was an npm module that would make this easier.

推荐答案

这是我的解决方案.请记住,日、周和月是相对于起源而言的,因为纪元:

Here is my solution. Keep in mind that day, week and month are relative to origin, since epoch:

let event = [ {id: 1, date: 1387271989749 }, {id:2, date: 1387271989760} ];

function groupday(value, index, array){
   let byday={};
    let d = new Date(value['date']);
    d = Math.floor(d.getTime()/(1000*60*60*24));
    byday[d]=byday[d]||[];
    byday[d].push(value);
  return byday
}

function groupweek(value, index, array){
  let byweek={};
   let d = new Date(value['date']);
    d = Math.floor(d.getTime()/(1000*60*60*24*7));
    byweek[d]=byweek[d]||[];
    byweek[d].push(value);
  return byweek
}

function groupmonth(value, index, array){
   let bymonth={};
    d = new Date(value['date']);
    d = (d.getFullYear()-1970)*12 + d.getMonth();
    bymonth[d]=bymonth[d]||[];
    bymonth[d].push(value);
  return bymonth
}

console.log('grouped by day', event.map(groupday));
console.log('grouped by week',event.map(groupweek));
console.log('grouped by month',event.map(groupmonth));


注意:对象是分组的,但日、周和月的键是数字的.只有年份的键是人类可读的.

NOTE: Objects are grouped but the keys for day, week and month are in digits. Only the key for year is human readable.

这篇关于如何按天、周、月对具有时间戳属性的对象进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)