使用AngularJS和LoDash从对象创建包含键和值的数组

时间:2014-11-27 18:06:28

标签: javascript angularjs lodash

我正在尝试在我的AngularJS应用中使用Angular-Chart

From the documentation,我发现你可以像这样定义标签,系列和数据:

$scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
$scope.series = ['Series A', 'Series B'];

$scope.data = [
  [65, 59, 80, 81, 56, 55, 40],
  [28, 48, 40, 19, 86, 27, 90]
];

我的问题是,在我的情况下,标签是来自对象的键值,而系列和数据是来自这些对象的值和属性。

例如,给出以下示例:

enter image description here

标签将是:['324', '369', '391', '396', 'KE-22'](来自汽车中所有对象的密钥)

系列将是:['Trips','Total'](来自汽车的价值)

数据将是(来自汽车的财产):

[
   ['1','4','5','3','7'],
   ['00:00:08','00:00:13','00:00:24','00:00:03','00:00:01']
]

enter image description here

我有什么方法可以创建这样的数组吗?

现在我使用LoDash创建对象,如下所示:

dataByMonth = _.mapValues(dataByMonth, function(month) {
    var obj = {};
    var carArray = [];
    carArray = _.groupBy(month, 'car');
    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;

0 个答案:

没有答案