根据数组中的小时计算日期的出现次数

时间:2015-10-02 13:52:24

标签: javascript jquery arrays datetime

我有以下方法 -

[INFO] ---------------------------------------------------- [ERROR]
BUILD FAILURE [INFO]
---------------------------------------------------- 
[INFO] Compilation failure Unable to locate the Javac Compiler in:  
C:\Program Files\Java\jdk1.6.0_20\..\lib\tools.jar Please ensure you
are using JDK 1.4 or above and not a JRE (the
com.sun.tools.javac.Main class is required). In most cases you can
change the location of your Java installation by setting the
JAVA_HOME environment variable.

然后按最早的日期排序 -

// Push the date values into dateArray
var dateArray = [];

    $.each(dateHtml, function (i, el) {
                dateArray.push(htmlDateValue);
        }
    });

让我离开 - 例如 -

// sort array to have earliest date first
    dateArray.sort(function (a, b) {
        return new Date(b.date) - new Date(a.date);
    });

我创建了dateArray = ["02/10/2015 00:00:07", "02/10/2015 00:00:08", "02/10/2015 01:00:03", "02/10/2015 01:00:05", "02/10/2015 02:00:14", "03/10/2015 07:00:37"]; ,我想填写每个中存在的每个小时的计数值。

因此,例如在前面提到的数组中,结果应为 -

var arrOfCounts = [];

所以我开始循环arrOfCounts = [2, // 2 instances of values within the hour of 00:00:00 on the 02/10/2015 2, // 2 instances of values within the hour of 01:00:00 on the 02/10/2015 1, // 1 instances of values within the hour of 02:00:00 on the 02/10/2015 1]; // 1 instances of values within the hour of 07:00:00 on the 03/10/2015 -

中的每个值
dateArray

我怎么能实现这个目标?

2 个答案:

答案 0 :(得分:1)

使用表示日期和时间的键创建自定义对象

var dates = ["02/10/2015 00:00:07",
  "02/10/2015 00:00:08",
  "02/10/2015 01:00:03",
  "02/10/2015 01:00:05",
  "02/10/2015 02:00:14",
  "03/10/2015 07:00:37"
];
var counts = {};

dates.forEach(function(date) {
  var dateS = date.split(' ')[0];
  var hour = new Date(date).getHours();

  if (typeof counts[dateS] == 'undefined') {
    counts[dateS] = {};
  }

  if (typeof counts[dateS][hour] == 'undefined') {
    counts[dateS][hour] = 0;
  }

  counts[dateS][hour] += 1;
});

console.log(counts);

答案 1 :(得分:0)

  

因此,例如在前面提到的数组中,结果应为 -

arrOfCounts  = 
[2,  // 2 instances of values within the hour of 00:00:00 on the 02/10/2015
 2,  // 2 instances of values within the hour of 01:00:00 on the 02/10/2015
 1,  // 1 instances of values within the hour of 02:00:00 on the 02/10/2015
 1]; // 1 instances of values within the hour of 07:00:00 on the 03/10/2015

尝试使用Array.prototype.map()String.prototype.match()RegExp /\d+:\d+/Array.prototype.filter()

var dateArray = ["02/10/2015 00:00:07"
                 , "02/10/2015 00:00:08"
                 , "02/10/2015 01:00:03"
                 , "02/10/2015 01:00:05"
                 , "02/10/2015 02:00:14"
                 , "03/10/2015 07:00:37"
                ];

var arrOfCounts = dateArray.map(function(val) {
                    return val.match(/\d+:\d+/)[0]
                  })
                  .map(function(count, i, array) {
                    return count !== array[i + 1] 
                           ? array.toString()
                             .match(new RegExp(count, "g")).length 
                           : null
                  })
                  .filter(Boolean);
console.log(arrOfCounts)