我有以下的方法 -計數出現基於小時在陣列
// 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);
}
});
我怎麼能做到這一點?
毫無疑問,開始與該處理日期/時間以及一個像樣的圖書館! [moment.js](http://momentjs.com/)通常很受歡迎 – Jamiec
可能的重複http://stackoverflow.com/questions/16292349/count-occurrences-of-array-objects – guest271314