【发布时间】:2014-11-28 15:00:37
【问题描述】:
给定以下对象:
Object {November 2014: Object}
November 2014: Object
Cars: Object
324: Object
duration: 1417132808000
total: "00:00:08"
trips: 1
369: Object
duration: 5668531247000
total: "00:00:47"
trips: 4
391: Object
duration: 9919930257000
total: "00:10:57"
trips: 7
396: Object
duration: 9919929791000
total: "00:03:11"
trips: 7
KE-22: Object
duration: 5668531269000
total: "00:01:09"
trips: 4
我需要能够从从中提取的数据中创建数组。
类似这样的:
标签:(车内物体的钥匙)
[324, 369, 391, 396, KE-22]
系列:(来自 Cars 中每个对象的属性)
[Trips, Total]
数据:(来自 Cars 中每个对象的属性值)
[
[1,4,7,7,4], // Array of trips for each car, in order.
["00:00:08", "00:00:47", "00:10:57", "00:03:11", "00:01:09"] // Array of total duration for each car, in order.
]
我打算用这个数组为每个月填充一个图表。
对象集合已使用以下代码创建,可能与问题相关:
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')
return sum;
}, {trips: 0, duration: 0, total:0})
});
})
return obj;
});
$scope.statistics = dataByMonth;
console.log($scope.statistics);
关于如何进行的任何提示?
【问题讨论】: