javascript - 2D Array or Map to store values against weekdays and slots within days -


i want increment values days of week , slots within each day, i.e. morning, afternoon, evening , night, iterate on collection time , evaluate timeslot time in. need able iterate on end result in front end want nicer solution 1 have represented totals array. has been suggested use map unsure of how implement map.

   var totals = [     //  tot m  t  w  t  f  s  s     [0, 0, 0, 0, 0, 0, 0, 0],    // totals     [0, 0, 0, 0, 0, 0, 0, 0],    // morning     [0, 0, 0, 0, 0, 0, 0, 0],    // afternoon     [0, 0, 0, 0, 0, 0, 0, 0],    // evening     [0, 0, 0, 0, 0, 0, 0, 0]     // night ];    var collectionofdata = entiredataset;   collectionofdata.foreach(function (item) {     var localdate = getlocaldate(item);//gets users local date , determines timeslot id - ie morning,afternoon, evening, or night     var dayofweek = localdate.day();     var timeslotid = item.timeslotid;      totals[timeslotid][dayofweek]++;  // increase sessions per slot , day     totals[0][dayofweek]++;           // increase total sessions per day     totals[timeslotid][0]++;    // increase total sessions per slot     totals[0][0]++;             // increase total sessions      } 

any suggestions appreciated.

one of ways design data structure follows - see demo input, output , total calculation methods:

var slots={'monday':{'morning':0,'afernoon':0,'evening':0,'night':0},'tuesday':{'morning':0,'afernoon':0,'evening':0,'night':0},'wednessday':{'morning':0,'afernoon':0,'evening':0,'night':0},'thursday':{'morning':0,'afernoon':0,'evening':0,'night':0},'friday':{'morning':0,'afernoon':0,'evening':0,'night':0},'saturday':{'morning':0,'afernoon':0,'evening':0,'night':0},'sunday':{'morning':0,'afernoon':0,'evening':0,'night':0},}    // input data  function insertslotfor(day, slot) {    slots[day][slot]++;  }    // output data  function getslotfor(day, slot) {    return slots[day][slot];  }    // total day  function totalforday(day) {    return object.keys(slots[day]).reduce(function(prev,curr){      return prev + slots[day][curr];    },0);  }    // total slot  function totalforslot(slot) {    return object.keys(slots).reduce(function(prev,curr){      return prev + slots[curr][slot];    },0);  }    insertslotfor('monday', 'morning');  insertslotfor('monday', 'night');  insertslotfor('tuesday', 'morning');    console.log(slots);    console.log(totalforday('monday'));  console.log(totalforslot('morning'));
.as-console-wrapper{top:0;max-height:100%!important;}


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -