Проблема с последним столбцом суммы средних значений - данные проанализированы с помощью JSON - Angular JS

0

У меня проблема со средней суммой. Мне нужно рассчитать среднее значение суммы с помощью следующей формулы. следующий код не вычисляет последний столбец Average. Я требую желаемого результата. Не могли бы вы мне помочь. Помощь определенно оценена.

формула

colTotal/(names[$index].arr_length*4) 

var arr = [{
  "unique_id": "CS", "college": "BSCS", "arr_length": 1,
  "program_section": [
    { "question": "Q1", "total": 135 },
    { "question": "Q2", "total": 142 },
    { "question": "Q3", "total": 135 },
    { "question": "Q4", "total": 137 }
  ]
}, {
  "unique_id": "MBA", "college": "BBA", "arr_length": 2,
  "program_section": [
    { "question": "Q1", "total": 175 },
    { "question": "Q2", "total": 142 },
    { "question": "Q3", "total": 165 },
    { "question": "Q4", "total": 137 }
  ]
}, {
  "unique_id": "CA", "college": "Account", "arr_length": 1,
  "program_section": [
    { "question": "Q1", "total": 145 },
    { "question": "Q2", "total": 162 },
    { "question": "Q3", "total": 125 },
    { "question": "Q4", "total": 117 }
  ]
}];
angular.module('myApp', []).controller('customersCtrl', function($scope, $http) {
    $scope.names = arr;
    $scope.totals = {};
    arr.forEach(function(obj){
        obj.program_section.forEach(function(section){
            $scope.totals[section.question] = ($scope.totals[section.question] || 0) + section.total
        });
    });

    // Sum each item in 'arr'.
    $scope.colTotals = $scope.names.map(function (name) {
        return name.program_section.reduce(function (total, section) {
            return total + section.total;
        }, 0);
    });

    // Add the sum of sums
    $scope.colTotals.push($scope.colTotals.reduce(function (x, y) {
        return x + y;
    }, 0));
}).filter('range', function() {
  return function(input, total) {
    total = parseInt(total);

    for (var i=0; i<total; i++) {
      input.push(i);
    }

    return input;
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl"> 

<table  border="1">
<thead>
  <tr>
    <td>Question</td>
    <td ng-repeat="x in names">{{ x.college }}</td>
    <td>Totals</td>
  </tr>
</thead>
<tbody>
      <tr ng-repeat="n in [] | range:4">
        <td>
            {{ names[0].program_section[n].question }}
        </td>
        <td width="100px" ng-repeat="x in names" >
           {{ x.program_section[n].total }}
        </td>
        <td width="50%" ng-bind="totals[names[0].program_section[n].question]"></td>
      </tr>
      <tr style="font-weight: bold;">
        <td>Totals</td>
        <td ng-repeat="colTotal in colTotals track by $index">{{ colTotal/(names[$index].arr_length*4) }}</td>
      </tr>
</tbody>
  
</table>

</div>

Желаемый результат

<table border="1">
  <thead>
    <tr>
      <td>Question</td>
      <td >BSCS</td>
      <td >BBA</td>
      <td >Account</td>
      <td>Totals</td>
    </tr>
  </thead>
  <tbody>
    <tr >
      <td> Q1 </td>
      <td width="100px"> 135 </td>
      <td width="100px"> 175 </td>
      <td width="100px"> 145 </td>
      <td width="50%">455</td>
    </tr>
    <tr >
      <td> Q2 </td>
      <td width="100px"> 142 </td>
      <td width="100px"> 142 </td>
      <td width="100px"> 162 </td>
      <td width="50%">446</td>
    </tr>
    <tr >
      <td> Q3 </td>
      <td width="100px"> 135 </td>
      <td width="100px"> 165 </td>
      <td width="100px"> 125 </td>
      <td width="50%">425</td>
    </tr>
    <tr >
      <td> Q4 </td>
      <td width="100px"> 137 </td>
      <td width="100px"> 137 </td>
      <td width="100px"> 117 </td>
      <td width="50%">391</td>
    </tr>
    <tr>
      <td>Totals</td>
      <td >137.25</td>
      <td >77.375</td>
      <td >137.25</td>
      <td >1717/4 =<strong> 429.25</strong></td>
    </tr>
  </tbody>
</table>
Теги:
sum
average

1 ответ

1
Лучший ответ

попробуйте this.when индекс 3, тогда имена [3].arr_length - undefind.

 <td ng-repeat="colTotal in colTotals track by $index" ng-if="!$last">{{ colTotal/(names[$index].arr_length*4) }}</td>
 <td ng-repeat="colTotal in colTotals track by $index" ng-if="$last">{{ colTotal/4 }}</td>
  • 0
    Спасибо, вы спасли мой день

Ещё вопросы

Сообщество Overcoder
Наверх
Меню