计算数组中的不同元素

时间:2013-05-24 08:18:51

标签: javascript jquery

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

假设我有这样的数据:

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
    }
}];

I've created a very simple code to simply aggregate holidays

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分割假期。


我的解决方案: My version of answer

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的修改版本,所以我不会将其作为独立答案发布 我发现我的版本更容易理解,希望有人发现它有用。

3 个答案:

答案 0 :(得分: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));

演示:Fiddle

答案 1 :(得分:1)

去寻找

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

而不是

console.log(summary.length);

因为您可以使用length属性获取js对象中的元素数。

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

您可以找到针对此问题的更多信息和解决方案in this answer

请参阅updated fiddle

答案 2 :(得分: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));

Fiddle here

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

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