2015-10-02 15 views
0

我有以下的方法 -计数出现基于小时在阵列

// 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 = [];,我想填充每个h的计数值我们的存在于每个中。

因此,例如上述的阵列的结果应该是 -

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每个值的循环 -

$.each(dateArray, function (i, el) { 

        // Here I have to establish the value of count 
        arrOfCounts.push(count); 
      } 
     }); 

我怎么能做到这一点?

+1

毫无疑问,开始与该处理日期/时间以及一个像样的图书馆! [moment.js](http://momentjs.com/)通常很受欢迎 – Jamiec

+0

可能的重复http://stackoverflow.com/questions/16292349/count-occurrences-of-array-objects – guest271314

回答

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);

+0

指出它做得好。 – Ebikeneser

+0

@Ebikeneser,很高兴它帮助:) – AmmarCSE

+0

钉住它...直到你在一个浏览器上尝试它的JavaScript日期的构造函数不接受该格式。不能告诉你,如果你是美国或英国的格式,但无论哪一个尝试交换你的浏览器到另一个,并使用一个看起来不正确的日期(例如'24/12/2015'或'12/24/2015') – Jamiec

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 

试试utilizin摹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)