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

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.

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do I can get a text of all the cells of the table using testcafe(如何使用 testcafe 获取表格中所有单元格的文本)
node_modules is not recognized as an internal or external command(node_modules 未被识别为内部或外部命令)
How can I create conditional test cases using Protractor?(如何使用 Protractor 创建条件测试用例?)
PhantomJS and clicking a form button(PhantomJS 并单击表单按钮)
Clicking #39;OK#39; on alert or confirm dialog through jquery/javascript?(在警报上单击“确定或通过 jquery/javascript 确认对话框?)
QunitJS-Tests don#39;t start: PhantomJS timed out, possibly due to a missing QUnit start() call(QunitJS-Tests 不启动:PhantomJS 超时,可能是由于缺少 QUnit start() 调用)