在JavaScript中按日期对DateTime对象进行分组和求和 [英] Group and sum DateTime object by date in JavaScript

查看:467
本文介绍了在JavaScript中按日期对DateTime对象进行分组和求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按日期对对象进行分组并将它们全部加起来以创建单个JavaScript对象的最佳方法是什么?与应用程序相比,我不喜欢数据密集型应用程序,因此我更喜欢代码的可读性和较少的代码行.而且,实习结束后,会有很多人要做出改变.

What is the best way to group objects by date and add them all up to create a single JavaScript object? I prefer code readability and lesser lines of code over performance as the application is not data intensive. Moreover there are going to be lots people who are going to make changes after I'm done with my internship.

以下是一些示例数据

[
    {"Timestamp":"Thu, 01 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
    {"Timestamp":"Thu, 01 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
    {"Timestamp":"Thu, 01 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
    {"Timestamp":"Thu, 01 Jan 2015 03:57:00 GMT","cp":0,"pc":4},

    {"Timestamp":"Thu, 02 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
    {"Timestamp":"Thu, 02 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
    {"Timestamp":"Thu, 02 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
    {"Timestamp":"Thu, 02 Jan 2015 03:57:00 GMT","cp":0,"pc":4}

]

我想从以上数据中创建出来.

I want to create this out of the above data.

[ {"Date":"2015-01-01", "cp":4, "pc":12}, {"Date":"2015-01-02", "cp":4, "pc"12} ]

如果您注意到,生成的JavaScript对象是该特定日期上所有列的总和,并用日期替换时间戳记.

If you notice, the resultant JavaScript object is the sum total of all the columns on that particular date and replace timestamp by date.

我们已经在应用程序中使用 moment.js ,所以我可以使用它.如果您认为片刻可以用更整洁的方式解决问题,请告诉我.

We already use moment.js in the application, so I have it at disposal. If you think moment can solve the problem in a neater fashion, please let me know.

推荐答案

  • 迭代对象数组($.each()函数可用于迭代任何集合.)
  • 使用toISOString()方法将UTC格式的日期字符串转换为ISO格式.
  • 维护日期的通用数组,以便您可以使用indexOf()方法轻松映射重复的日期位置.
    • Iterate your array of objects ($.each() function can be used to iterate over any collection.)
    • Convert UTC format date string to ISO format using toISOString() method.
    • Maintain common array for date so that you can easily map your repeated date location using indexOf() method.
    • 以下是代码:

      var arr = [
        {"Timestamp":"Thu, 01 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
        {"Timestamp":"Thu, 01 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
        {"Timestamp":"Thu, 01 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
        {"Timestamp":"Thu, 01 Jan 2015 03:57:00 GMT","cp":0,"pc":4},
      
        {"Timestamp":"Thu, 02 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
        {"Timestamp":"Thu, 02 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
        {"Timestamp":"Thu, 02 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
        {"Timestamp":"Thu, 02 Jan 2015 03:57:00 GMT","cp":0,"pc":4}
      ];
      var resultArr = [];
      var dateArr = [];   
      
      $.each(arr, function () {
      // Easiest way to get your required date format
         var date = new Date(this.Timestamp).toISOString().replace(/T/, ' ').split(' ')[0];
         var index = dateArr.indexOf(date);
         if (index == -1) {
             dateArr.push(date);
             var obj = {Date: date, cp: this.cp, pc: this.pc};
             resultArr.push(obj);
         } 
         else {
             resultArr[index].cp += this.cp;
             resultArr[index].pc += this.pc;
         }
      });
      console.log(resultArr); // [ {"Date":"2015-01-01", "cp":4, "pc":12}, {"Date":"2015-01-02", "cp":4, "pc"12} ]
      

      希望这对您有所帮助.

      这篇关于在JavaScript中按日期对DateTime对象进行分组和求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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