0

我有从我的服务器到数据表的数据。我成功地填充了我的表格,但在页脚回调中我想做一些统计。

可以说我有这样的数据:

var data = [{
    date: '2013-05-12',
    holiday: "One type of holiday",
    dayType: "Weekend"
}, {
    date: '2013-05-13',
    holiday: "Another type",
    dayType: "Weekend"
}, {
    date: '2013-05-14',
    holiday: "Another type",
    dayType: "Work"
}, {
    date: '2013-05-15',
    holiday: "",
    dayType: "Work"
}];

我想计算不同假期的天数。

这是我想得到的结果:

var summary= [
{
    "One type of holiday": {
        "work": 0,
        "weekend": 1
    }
},
{
    "Another type": {
        "work": 1,
        "weekend": 1
    }
}];

我创建了一个非常简单的代码来简单地聚合假期

for (var i = 0; i < data.length; i++) {
    //console.log(data[i].holiday);
    /*other stuff here*/
    if (data[i].holiday.length > 0) 
        summary[data[i].holiday] = summary[data[i].holiday] + 1 || 1;
}

但这给了我无效的结果,因为在我的数据数组中,假期包含空格。

我需要一种方法来解决这个问题并根据 dayType 拆分假期。


我的解决方案: 我的答案版本

var summary = {}, d, tmp, type;
for (var i = 0; i < data.length; i++) {
    var d = data[i];
    if (d.holiday.length > 0) {
        type = d.dayType == 'Weekend' || d.dayType == 'Free' ? 'Weekend' : 'Work';
        tmp = summary[d.holiday];
        if (!tmp) {
            tmp = {
                Weekend: 0,
                Work: 0
            };
            summary[d.holiday] = tmp;
        }
        summary[d.holiday][type] += 1;
    }
}

因为这是@Arun 答案的修改版本,所以我不会将其作为独立答案发布。
我发现我的版本更容易理解,希望有人觉得它有用。

4

3 回答 3

2

尝试

var summary = [], summaryMap = {}, d, map, m;
for (var i = 0; i < data.length; i++) {
    var d = data[i];
    map = summaryMap[d.holiday];
    if(!map){
        map = {
            Work: 0,
            Weekend: 0
        };
        m = {};
        m[d.holiday] = map;
        summary.push(m);
        summaryMap[d.holiday] = map;
    }
    map[d.dayType] += 1;
}
console.log(summary);
console.log(JSON.stringify(summary));

演示:小提琴

于 2013-05-24T08:29:33.730 回答
1

去做

console.log(Object.keys(summary).length);

代替

console.log(summary.length);

因为可以通过length属性获取一个js对象中元素的个数。

注意:使用 Object.keys 可能会导致浏览器兼容性问题。作为其支持的形式 IE 9 和 Firefox 4。在此MDN 文章中查看更多信息。

您可以在此答案中找到有关此问题的更多信息和解决方案。

查看更新的小提琴

于 2013-05-24T08:29:37.517 回答
0

这是我的尝试:

var summary = [];
var holidayTypes = [];
var dayTypes = [];

//first work out the different types of holidays
for (var i = 0; i < data.length; i++) {
   if(holidayTypes.indexOf(data[i].holiday) == -1){
       //this is a new type of holiday
       holidayTypes.push(data[i].holiday);
   }
   if(dayTypes.indexOf(data[i].dayType) == -1){
       //new type of day. 
       dayTypes.push(data[i].dayType);
   }
}
console.log('types of holiday: ' + JSON.stringify(holidayTypes));
console.log('types of day: ' + JSON.stringify(dayTypes));


for(index in holidayTypes){
    var typeobj = {};
    //create an object for each type of holiday
    typeobj[holidayTypes[index]] = {};

    for(index2 in dayTypes){
        //initialize a count for each type of day
        typeobj[holidayTypes[index]][dayTypes[index2]] = 0;
        //iterate through the data and count the occurrences where the day AND holiday match.
        //if they do, iterate the value.
        for (var j = 0; j < data.length; j++){
            if((data[j].holiday == holidayTypes[index]) 
                && (data[j].dayType == dayTypes[index2])){
                typeobj[holidayTypes[index]][dayTypes[index2]]++;                        
            }
        }
    }
    summary.push(typeobj);
}
console.log(JSON.stringify(summary));

在这里提琴

输出: [{"One type of holiday":{"Weekend":1,"Work":0}},{"Another type":{"Weekend":1,"Work":1}},{"":{"Weekend":0,"Work":1}}]

它有效,但不太可能像上面的人那样高效!

于 2013-05-24T08:56:26.680 回答