如何将数据点列表分组并按天汇总?例如,给出这个列表:
2012-03-18T00:00:04
2012-03-18T00:05:03
2012-03-19T00:10:04
2012-03-19T00:15:03
2012-03-19T00:20:03
2012-03-19T00:25:03
我希望:
2012-03-18,2
2012-03-19,4
答案 0 :(得分:1)
假设您将数据点作为名为points
var map = {};
points.forEach(function(x) {
var s = x.split("T")[0];
if(map.hasOwnProperty(s)) {
map[s]++;
}
else {
map[s] = 1;
}
});
这将为您计算该日期的每次出现次数。
实施例
js> points
["2012-03-18T00:00:04", "2012-03-18T00:05:03", "2012-03-19T00:10:04", "2012-03-19T00:15:03", "2012-03-19T00:20:03", "2012-03-19T00:25:03"]
js> points.forEach(function(x) {
var s = x.split("T")[0];
if(map.hasOwnProperty(s)) {
map[s]++;
}
else {
map[s] = 1;
}
});
js> map
({'2012-03-18':2, '2012-03-19':4})
答案 1 :(得分:0)
我必须假设T
将日期与时间分开。您可以使用正则表达式或子字符串提取日期,并添加到计数器中。我使用了正则表达式。
var count = {};
var input = [
"2012-03-18T00:00:04",
"2012-03-18T00:05:03",
"2012-03-19T00:10:04",
"2012-03-19T00:15:03",
"2012-03-19T00:20:03",
"2012-03-19T00:25:03"
];
for(var i = 0; i < input.length; i++) {
var date = /(.*)T/.exec(input[i])[1];
if(!count[date]) {
count[date] = 1;
}
else {
count[date] += 1;
}
}
for (var date in count) {
document.write(date+","+count[date]+"<br>");
}