Utilizing LoDash to generate statistics from records stored in an IndexedDB.
$scope.refreshStats = function() {
var dataByMonth = _.groupBy($scope.stats, function(record) {
return moment(record.date).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')
return sum;
}, {trips: 0, duration: 0, total:0})
});
})
return obj;
});
$scope.statistics = dataByMonth;
console.log($scope.statistics);
};
The output of the function consists of nested objects with keys representing a month and year combination:
Object {
July 2016: Object,
August 2016: Object,
September 2016: Object,
October 2016: Object
}
An issue arises when displaying this data on the frontend as the ng-repeat directive arranges the months alphabetically (e.g., August-July-September-October) instead of chronologically. Is there a way to sort it by date?
This is how the ng-repeat loop is structured:
<div ng-repeat="(monthName, monthValue) in statistics">
{{monthName}}
</div>
Is it possible to utilize orderBy:date
when the date serves as the object key?
EDIT
My specific query pertains to identifying the Key as Date and then arranging it accordingly. Existing suggestions have not resolved this particular challenge.