0

Following data is i am getting.

var data =[{"Type1":2,"Type2":0,"Type3":6,"Date":"2015-01-27","Source":"Web"},
          {"Type1":2,"Type2":2,"Type3":0,"Date":"2015-02-27","Source":"Mobile"},
          {"Type1":3,"Type2":0,"Type3":7,"Date":"2015-02-27","Source":"Unknown"},
          {"Type1":4,"Type2":5,"Type3":9,"Date":"2015-02-27","Source":"Web"}]

I want to sum this data by doing Group by Date like this,

var result= 
              [{"Type1":2,"Type2":0,"Type3":6,"Date":"2015-01-27"},
              {"Type1":9,"Type2":7,"Type3":16,"Date":"2015-02-27"}]

For this i have written following jquery code, but its not working. Please suggest me changes.

    var objects = JSON.stringify(data)
      var grphDates = new Array();
            var groupedObjects = new Array();
            $.each(objects, function (ix, obj) {
                var existingObj;
                if ($.inArray(obj.Date, grphDates) >= 0) {
                    existingObj = $.grep(groupedObjects, function (o, si) { return o.Date === obj.Date; });
                    existingObj.Type1 += obj.Type1;
                    existingObj.Type2 += obj.Type2;
                    existingObj.Type3 += obj.Type3;
                  } else {
                    groupedObjects.push(obj);
                    grphDates.push(obj.Date);
                }
            });
1

2 Answers 2

1

Data is already an array of objects so no need to do JSON.stringify(data)

Also take into consideration that grep returns an array of objects (http://api.jquery.com/jquery.grep/)

This changes should do it

      var data =[{"Type1":2,"Type2":0,"Type3":6,"Date":"2015-01-27","Source":"Web"},
          {"Type1":2,"Type2":2,"Type3":0,"Date":"2015-02-27","Source":"Mobile"},
          {"Type1":3,"Type2":0,"Type3":7,"Date":"2015-02-27","Source":"Unknown"},
          {"Type1":4,"Type2":5,"Type3":9,"Date":"2015-02-27","Source":"Web"}];

      var grphDates = new Array();
      var groupedObjects = new Array();
      $.each(data, function (ix, obj) {
           var existingObj;
           if ($.inArray(obj.Date, grphDates) >= 0) {
                // Get index of existing object width date == obj.Date
                var index = groupedObjects.map(function(o, i) { 
                               if(o.Date == obj.Date)return i;
                               }).filter(isFinite);

                groupedObjects[index].Type1 += obj.Type1;
                groupedObjects[index].Type2 += obj.Type2;
                groupedObjects[index].Type3 += obj.Type3;
           } else {
                groupedObjects.push(obj);
                grphDates.push(obj.Date);
           }
        });
Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at using lodash's groupBy method. It seems to do almost exactly what you're looking for:

_.groupBy(data, 'Date');

returns

{ '2015-01-27': [ 
     { Type1: 2, 
       Type2: 0, 
       Type3: 6, 
       Date: '2015-01-27', 
       Source: 'Web' 
      } 
  ],
  '2015-02-27': 
   [ { Type1: 2,
       Type2: 2,
       Type3: 0,
       Date: '2015-02-27',
       Source: 'Mobile' },
     { Type1: 3,
       Type2: 0,
       Type3: 7,
       Date: '2015-02-27',
       Source: 'Unknown' },
     { Type1: 4, 
       Type2: 5, 
       Type3: 9, 
       Date: '2015-02-27', 
       Source: 'Web' 
     } 
   ] 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.