[Chartjs]-How do I recursively aggregate values inside of a loop of JavaScript objects?

3👍

Try like this with for..in loop

var orderedTimeSeriesObject = {
    1573538400: { // Tue, 12 Nov 2019 06:00:00
        clicks: 14,
        signups: 20
    },
    1574316000: { // Thu, 21 Nov 2019 06:00:00
        clicks: 13,
        signups: 15
    },
    1574488800: { // Sat, 23 Nov 2019 06:00:00
        clicks: 0,
        signups: 0
    }
};
var lastKey =0;
var lastValue ={clicks:0,signups:0};
for (key in orderedTimeSeriesObject){
   if(lastKey<key){
      orderedTimeSeriesObject[key].clicks+= lastValue.clicks;
      orderedTimeSeriesObject[key].signups+= lastValue.signups;
     lastValue = orderedTimeSeriesObject[key]; 
   }
   lastKey = key;
   
}

console.log(orderedTimeSeriesObject);

Leave a comment