javascript - 垂直求和从 JSON 解析的当前结果值 - Angular JS

标签 javascript jquery angularjs json sum

我有一个 Angular JS Controller ,它对水平方向的值结果求和,但现在我需要对垂直方向的值的当前结果求和。这个结果是从 JSON 解析出来的,我也尝试过很多过滤器,但没有成功。不知道如何计算从 View 到 Controller 的结果集。我需要想要的结果。绝对感谢您的帮助。

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,
        
      },
      
    ]
  }
];
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $scope.names = arr;
    $scope.totals = {}
    arr.map(function(obj){
        obj.program_section.map(function(section){
            $scope.totals[section.question] = ($scope.totals[section.question] || 0) + section.total
        })
    })
}).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>
      
</tbody>
  
</table>

</div>

Desired Result

<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><strong>Total</strong></td>
      <td ><strong>549</strong></td>
      <td width="100px" ><strong>619</strong></td>
      <td width="100px" ><strong>549</strong></td>
      <td width="50%"><strong>1717</strong></td>
    </tr>
  </tbody>
</table>

最佳答案

这应该可以实现您的目标:

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 }}</td>
      </tr>
</tbody>
  
</table>

</div>

关于javascript - 垂直求和从 JSON 解析的当前结果值 - Angular JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36140819/

相关文章:

javascript - 缩小的 JS 导致 JS 无法工作

javascript - 解析XML以获取节点值

angularjs - 如何配置 Spring 和 Angular 协同工作

javascript - 更改timelineJS源码

javascript - 使用 slideToggle 隐藏/显示高级选项,子元素可见

javascript - JS : What do the curly braces inside function parameter declarations mean?

javascript - jQuery 相关问题。正在执行的默认操作、Javascript 警告和 slideDown() 很奇怪

jquery - Jquery Accordion 问题中的 2 个图像映射

c# - 从 angularJS 中的 aspx.cs 页面获取数据?

javascript - 如果负数不是一个因素,那么与零比较是否有可量化的好处?