javascript - 如何在javascript中按小时/6小时/天/周/月/年对日期对象进行分组

标签 javascript arrays grouping

我有 10 年的记录。所以 API 将返回我需要根据日期时间戳对数组进行分组的数组。用户可以选择分组选项作为

  • 小时
  • 6 小时(每天)
  • 年份

数据示例

[{
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.18809978382007495
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.42347719862654404
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.365386421616013
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.49364744650351033
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.31133576985952016
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.5509062071104307
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.5556739085429424
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.6668348570127745
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.4908101365372941
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.6042127351503115
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.3725497529313371
}, {
  "x": "2019-02-05T14:28:45.540Z",
  "y": 0.29687095332790064
}, {
  "x": "2018-02-07T11:19:19.034Z",
  "y": 0
}]

是否有任何通用的解决方案来按小时/天/周/月/年进行分组

示例输出我想要的。

小时

{
 "OCT 2th 2012 6AM": [
    "2012-10-02 06:00:00",
    "2012-10-10 06:03:51"
  ],
  "NOV 11th 2012 AM": [
    "2012-11-11 09:07:21",
    "2012-11-10 09:03:51"
  ],
  "OCT 6th 2013 2PM": [
    "2013-10-07 02:07:02"
  ],
}

每 6 小时

{
 "OCT 6th 2012 first 6 hours": [
    "2012-10-11 06:00:00",
  ],
 "OCT 6th 2012 second 6 hours": [
    "2012-10-10 06:03:51"
  ],
  "NOV 6th 2012 AM": [
    "2012-11-11 09:07:21",
    "2012-111-10 09:03:51"
  ],
  "OCT 6th 2013 2PM": [
    "2013-10-07 02:07:02"
  ],
}

一天

{
 "OCT 2th 2012": [
    "2012-10-2 06:00:00",
  ],
 "OCT 10th  2012": [
    "2012-10-10 06:03:51"
  ],
  "NOV 11th 2012": [
    "2012-11-11 09:07:21",
    "2012-11-10 09:03:51"
  ],
  "OCT 7th 2013": [
    "2013-10-07 02:07:02"
  ],
}

.for week

{
 "OCT 2012 week number": [
    "2012-10-2 06:00:00",
  ],
 "OCT 2012 week number": [
    "2012-10-10 06:03:51"
  ],

}

月份

{
 "OCT 2012 ": [
    "2012-10-2 06:00:00",
  ],
 "NOV 2012": [
    "2012-11-10 06:03:51"
  ],

}

最佳答案

您可以通过以下方式实现吗?

const data = [{"x":"2019-02-05T14:28:45.540Z","y":0.18809978382007495},{"x":"2019-02-05T14:28:45.540Z","y":0.42347719862654404},{"x":"2019-02-05T14:28:45.540Z","y":0.365386421616013},{"x":"2019-02-05T14:28:45.540Z","y":0.49364744650351033},{"x":"2019-02-05T14:28:45.540Z","y":0.31133576985952016},{"x":"2019-02-05T14:28:45.540Z","y":0.5509062071104307},{"x":"2019-02-05T14:28:45.540Z","y":0.5556739085429424},{"x":"2019-02-05T14:28:45.540Z","y":0.6668348570127745},{"x":"2019-02-05T14:28:45.540Z","y":0.4908101365372941},{"x":"2019-02-05T14:28:45.540Z","y":0.6042127351503115},{"x":"2019-02-05T14:28:45.540Z","y":0.3725497529313371},{"x":"2019-02-05T14:28:45.540Z","y":0.29687095332790064},{"x":"2018-02-07T11:19:19.034Z","y":0}];

const groups = (() => {
    const byDay = (item) => moment(item.x).format('MMM DD YYYY'),
        forHour = (item) => byDay(item) + ' ' + moment(item.x).format('hh a'),
        by6Hour = (item) => {
            const m = moment(item.x);
            return byDay(item) + ' ' + ['first', 'second', 'third', 'fourth'][Number(m.format('k')) % 6] + ' 6 hours';
        },
        forMonth = (item) => moment(item.x).format('MMM YYYY'),
        forWeek = (item) => forMonth(item) + ' ' + moment(item.x).format('ww');
    return {
        byDay,
        forHour,
        by6Hour,
        forMonth,
        forWeek,
    };
})();

const currentGroup = 'forWeek';
console.log(_.groupBy(data, groups[currentGroup]));
<script src="https://lodash.com/vendor/cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
<script src="https://momentjs.com/static/js/global.js"></script>

关于javascript - 如何在javascript中按小时/6小时/天/周/月/年对日期对象进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54572298/

相关文章:

javascript - Telegram - Node 。 JS处理多次更新

javascript - ajax文件下载 : progress event, 进行下载

java - 收集器分组由 java 8

javascript - 返回传递给函数的所有参数的总和

javascript - Angular js $scope 在 Jquery 函数中不起作用

c++ - 如何将 char 数组中的前 15 个字符转换为 C++ 中的 std::string?

php - 在 PHP 的 "array"语句中使用单词 "use"

javascript - 按子属性(可能缺失)对对象的 javascript 数组进行排序

PHP、MySQL : Need to show Parent name only once. 。差不多了,但仍然有问题

python - 对列表中连续出现的相同重复项进行分组