2015-10-02 14 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)