2015-02-08 99 views
0

我已将自己编码到一个角落,并且无法计算出如何计算小计(每种作物)和总计(所有作物)。我现在有硬编码的预期值,但需要弄清楚如何计算它们。AngularJS:小计和总计

I have a plunker

我正在使用budget.json模拟对工厂数据库的调用(在budget.js中定义)。 BudgetsController也在budget.js中定义。

硬编码总数从budgets.js的第35行开始。我尝试了几种LoDash方法来计算总数,但似乎无法找到我可以为每种作物复制的模式,并且我知道“总计”总数将遵循相同的模式,但仅使用小计。

任何帮助表示赞赏!

budget.js代码:

(function(){ 
    'use strict'; 
    angular 
    .module('ARM') 
    .factory('ExpensesFactory', function ExpensesFactory(
     $http, $q 
    ) { 

     return { 
     getBudget: getBudget 
     }; 

     function getBudget(id){ 
     return $http.get('budget.json'); 
     } 

    }) 
     .controller('BudgetsController', BudgetsController); 

    BudgetsController.$inject = ['$scope', 'ExpensesFactory']; 

    function BudgetsController(
     $scope, ExpensesFactory 
    ){ 
     ExpensesFactory.getBudget('1') 
     .then(function success(rsp){ 
      var arr = rsp.data; 
      var flattened = _.flatten(arr); 

      var grped = _.groupBy(flattened, function(item) { 
      return item.crop; 
      }); 
      $scope.uses = grped; 

      //TODO: Crop and Loan Budget Totals 
      $scope.uses.totals = [ 
      //CORN 
      [ 
       { 
       "arm": 178, 
       "dist": 197.91, 
       "other": 115, 
       "peracre": 490.91, 
       "calc_arm": 61837.2, 
       "calc_dist": 68753.934, 
       "calc_other": 39951, 
       "calc_total": 170542.134 
       } 
      ], 
      //SOYBEANS 
      [ 
       { 
       "arm": 145, 
       "dist": 69.73, 
       "other": 74.35, 
       "peracre": 289.08, 
       "calc_arm": 84143.5, 
       "calc_dist": 40464.319, 
       "calc_other": 43145.305, 
       "calc_total": 167753.124 
       } 
      ], 
      //SORGHUM 
      [ 
       { 
       "arm": 0, 
       "dist": 0, 
       "other": 0, 
       "peracre": 0, 
       "calc_arm": 0, 
       "calc_dist": 0, 
       "calc_other": 0, 
       "calc_total": 0 
       } 
      ], 
      //WHEAT 
      [ 
       { 
       "arm": 0, 
       "dist": 0, 
       "other": 0, 
       "peracre": 0, 
       "calc_arm": 0, 
       "calc_dist": 0, 
       "calc_other": 0, 
       "calc_total": 0 
       } 
      ], 
      //COTTON 
      [ 
       { 
       "arm": 0, 
       "dist": 0, 
       "other": 0, 
       "peracre": 0, 
       "calc_arm": 0, 
       "calc_dist": 0, 
       "calc_other": 0, 
       "calc_total": 0 
      } 
      ], 
      //RICE 
      [ 
       { 
       "arm": 0, 
       "dist": 0, 
       "other": 0, 
       "peracre": 0, 
       "calc_arm": 0, 
       "calc_dist": 0, 
       "calc_other": 0, 
       "calc_total": 0 
       } 
      ], 
      //PEANUTS 
      [ 
       { 
       "arm": 0, 
       "dist": 0, 
       "other": 0, 
       "peracre": 0, 
       "calc_arm": 0, 
       "calc_dist": 0, 
       "calc_other": 0, 
       "calc_total": 0 
       } 
      ], 
      //SUGAR CANE 
      [ 
       { 
       "arm": 0, 
       "dist": 0, 
       "other": 0, 
       "peracre": 0, 
       "calc_arm": 0, 
       "calc_dist": 0, 
       "calc_other": 0, 
       "calc_total": 0 
      } 
      ], 
      //TOTALS 
      [ 
       { 
       "arm": 0, 
       "dist": 0, 
       "other": 0, 
       "peracre": 0, 
       "calc_arm": 155999, 
       "calc_dist": 36530, 
       "calc_other": 87223, 
       "calc_total": 279752 
       } 
      ] 
      ]; 

      var uniqExp = _.uniq(_.pluck(flattened, 'expense')); 
      $scope.exp = uniqExp; 
     }); 
    } // end BudgetsController fn 
})(); 

回答

1

让我们来看看你有什么:

  1. _.groupBy:返回一个对象,它的键是作物的名称;
  2. _.map:迭代grped键(裁切名称)并返回一个数组; ():本地数组方法,通过整个数组积累一些值
    1. current:reduce每次从数组传递一个不同的元素;
    2. 前一个:这一个我们是在控制中,它第一次包含第二个参数reduce()的值,这就是为什么我传递了一个固定的对象。每次迭代都会修改此对象。

更多有关Array.prototype.reducehttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

样品:

 $scope.uses = _.map(grped, function (item, key) { 
     return item.reduce(function (previous, current) { 
      // in the nth iteration, each previous property will be equal to sum(arr[0].property...arr[n-1].property) 
      previous.arm += current.arm; 
      previous.dist += current.dist; 
      previous.other += current.other; 
      // other fields should be summed here 

      // remember to return the accumulator 
      return previous; 
     }, 
      /* initialize each property to zero, otherwize it won't work */ 
      {crop: key, arm: 0, dist: 0, other: 0}); 
     }); 
+0

你能解释一下你的过去和目前使用的? – jgravois 2015-02-08 23:35:53

+0

GREAT for SubTotals(http://plnkr.co/edit/fSZ7wUzySDNm1yqhxb2x?p=preview),但我不知道如何获得TOTALS现在我有SubTotals。我以为我可以做同样的事情,但传递$ scope.uses.total但这不适合我。 – jgravois 2015-02-08 23:58:26

+1

你几乎到了那里,但是你应该在你的脑海中建模(或逐步调试)为什么在每个reduce()周围使用map()。在总数的情况下,你只需要减少步骤。最后一点,避免将总数设置为数组的属性,直接将其保存到$ scope。 http://plnkr.co/edit/GeYpw7c0d6I3fgXy7lFF?p=preview – 2015-02-09 00:21:56