I am currently developing an AngularJS Application where I am inserting records in the form of objects into an array.
After using LoDash to extract statistics from this data (you can find the relevant question here), I encountered an issue. The stats only update when I reload the page, but as a Single Page Application (SPA), I need them to update dynamically as I add new records to the array ($scope.recordlist
).
Below is the key portion of the code:
var dataByMonth = _.groupBy($scope.recordlist, function(record) {
return moment(record.date, 'DD-MM-YYYY').format('MMMM YYYY');
});
dataByMonth = _.mapValues(dataByMonth, function(month) {
var obj = {};
obj.Cars = _.groupBy(month, 'car');
obj.Drivers = _.groupBy(month, 'driver');
_.each(obj, function(groupsValue, groupKey) {
obj[groupKey] = _.mapValues(groupsValue, function(groupValue) {
return _.reduce(groupValue, function(sum, trip) {
sum['trips']++;
sum['duration']+= moment.utc(trip.duration, 'HH:mm:ss');
sum['total'] = moment.utc(sum.duration). format('HH:mm:ss')
//addDuration(sum.duration, car.duration);
return sum;
}, {trips: 0, duration: 0, total:0})
});
})
return obj;
});
$scope.statistics = dataByMonth;
Can you help me identify what I missed?