array agregation

JavaScript

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 = [];
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));