Skip to content Skip to sidebar Skip to footer

Group And Sum Datetime Object By Date In Javascript

What is the best way to group objects by date and add them all up to create a single JavaScript object? I prefer code readability and lesser lines of code over performance as the a

Solution 1:

  • Iterate your array of objects ($.each() function can be used to iterate over any collection.)
  • Convert UTC format date string to ISO format using toISOString() method.
  • Maintain common array for date so that you can easily map your repeated date location using indexOf() method.

Here is the Code:

var arr = [
  {"Timestamp":"Thu, 01 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
  {"Timestamp":"Thu, 01 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
  {"Timestamp":"Thu, 01 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
  {"Timestamp":"Thu, 01 Jan 2015 03:57:00 GMT","cp":0,"pc":4},

  {"Timestamp":"Thu, 02 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
  {"Timestamp":"Thu, 02 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
  {"Timestamp":"Thu, 02 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
  {"Timestamp":"Thu, 02 Jan 2015 03:57:00 GMT","cp":0,"pc":4}
];
var resultArr = [];
var dateArr = [];   

$.each(arr, function () {
// Easiest way to get your required date formatvar date = newDate(this.Timestamp).toISOString().replace(/T/, ' ').split(' ')[0];
   var index = dateArr.indexOf(date);
   if (index == -1) {
       dateArr.push(date);
       var obj = {Date: date, cp: this.cp, pc: this.pc};
       resultArr.push(obj);
   } 
   else {
       resultArr[index].cp += this.cp;
       resultArr[index].pc += this.pc;
   }
});
console.log(resultArr); // [ {"Date":"2015-01-01", "cp":4, "pc":12}, {"Date":"2015-01-02", "cp":4, "pc"12} ]

Hope this may helpful for you.

Solution 2:

Try this.

var timeArray=[
    {"Timestamp":"Thu, 01 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
    {"Timestamp":"Thu, 01 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
    {"Timestamp":"Thu, 01 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
    {"Timestamp":"Thu, 01 Jan 2015 03:57:00 GMT","cp":0,"pc":4},

    {"Timestamp":"Thu, 02 Jan 2015 00:57:00 GMT","cp":2,"pc":2},
    {"Timestamp":"Thu, 02 Jan 2015 01:57:00 GMT","cp":0,"pc":4},
    {"Timestamp":"Thu, 02 Jan 2015 02:57:00 GMT","cp":2,"pc":2},
    {"Timestamp":"Thu, 02 Jan 2015 03:57:00 GMT","cp":0,"pc":4}
]

varobject = {}, resultArray = [];

for (var i = 0; i < timeArray.length; i++) {
    var date = timeArray[i].Timestamp;
    var localDateString = newDate(date).getFullYear() + '-' + (newDate(date).getMonth() + 1) + '-' + newDate(date).getDate();
    if (object[localDateString]) {
        object[localDateString].cp += timeArray[i].cp;
        object[localDateString].pc += timeArray[i].pc;
    } else {
        timeArray[i].Date = localDateString;
        delete timeArray[i].Timestamp;
        object[localDateString] = timeArray[i];
    }
}

for (var prop inobject)
    resultArray.push(object[prop]);

console.log(resultArray);

Solution 3:

I believe a simple utility library such as Underscore.js or Lodash could do the job, however, this looks like a task plain old vanilla Javascript can take on by itself.

Check out the Array.prototype.map function, or probably better yet an Array.prototype.sort that takes in a sorting function applying logic to a given object's .Timestamp property.

Edit: something like

yourArray.sort(function(objA, objB){
  return objA.Timestamp > objB.TimeStamp ? -1 : 1;
});

may work.

EDIT: I see now you are trying to merge dates. In that case, I would say Array.prototype.map chained with Array.prototype.reduce is a possible path to go down. You could also (messily) use Array.prototype.filter to push all arrays of a given date into a single array, and so on for each date.

Edit:

How about a reduce, then?

var thingYouWant = yourArray.reduce(function(summed, current){
    date = changeIntoOnlyDayDate(current.Timestamp);
    var found = false;
    summed.forEach(function(obj){
        if(obj.Timestamp === date){ obj.cp++; found = true;};
    }
    if(!found){summed.push(turnThisDateIntoDesiredObject(date))}

    }
    return summed;
    }, []); 

Post a Comment for "Group And Sum Datetime Object By Date In Javascript"