【发布时间】:2016-10-05 13:01:02
【问题描述】:
我正在使用 LoDash 从 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);
};
我的函数的结果是嵌套对象的集合,其键始终是一个月和一年:
Object {
July 2016: Object,
August 2016: Object,
September 2016: Object,
October 2016: Object
}
问题是,当我在前端显示它时,按月份分组的第一个 ng-repeat monthName 将按字母顺序排列(显示 August-July-September-October),到目前为止我还没有弄清楚如何按日期订购。
下面是 ng-repeat 的样子:
<div ng-repeat="(monthName, monthValue) in statistics">
{{monthName}}
</div>
当日期是对象的关键时,有什么方法可以使用orderBy:date?
编辑
这个问题不再重复,因为我的问题是需要将键标识为日期,然后按它排序。我无法通过引用问题的答案解决此问题。
【问题讨论】:
标签: javascript angularjs date angularjs-ng-repeat lodash