【问题标题】:Sum the value from json array of object将对象的json数组中的值相加
【发布时间】:2017-02-22 07:42:27
【问题描述】:
{ "Chemistry - II": [ { "id": "9", "title": "Solid State", "quecount": 12 }, { "id": "10", "title": "Solutions", "quecount": 9 }, { "id": "11", "title": "Electrochemistry", "quecount": 8 }, { "id": "6", "title": "d and f- Block elements", "quecount": 42 } ], "Physics": [ { "id": "3", "title": "Circular Motion", "quecount": 5 } ] }

我正在尝试对“quecount”求和,但没有得到结果

$scope.chlist;
    for (i = 0; i < $scope.chlist.length; i++) 
    {  
        total += $scope.chlist[i].quecount; 
    }

    $scope.totque = total;

我们将不胜感激。 谢谢。。

【问题讨论】:

  • 您需要化学和物理的总和还是全部的总和?

标签: angularjs arrays json


【解决方案1】:

您可以使用Array.prototype.map()Array.prototype.reduce()

let chlist = {
  "Chemistry - II": [{
    "id": "9",
    "title": "Solid State",
    "quecount": 12
  }, {
    "id": "10",
    "title": "Solutions",
    "quecount": 9
  }, {
    "id": "11",
    "title": "Electrochemistry",
    "quecount": 8
  }, {
    "id": "6",
    "title": "d and f- Block elements",
    "quecount": 42
  }],
  "Physics": [{
    "id": "3",
    "title": "Circular Motion",
    "quecount": 5
  }]
};

Object.values(chlist).forEach(function(v){
  console.log(v.map(o => o.quecount).reduce((a,b) => a + b));
});

【讨论】:

    【解决方案2】:

    使用angular.forEach 循环遍历所有groups(例如:Chemistry - II),对于您要计算quecount 的每个组。

    在您的控制器上:

    $scope.chlist = {
        "Chemistry - II": [{
          "id": "9",
          "title": "Solid State",
          "quecount": 12
        }, {
          "id": "10",
          "title": "Solutions",
          "quecount": 9
        }, {
          "id": "11",
          "title": "Electrochemistry",
          "quecount": 8
        }, {
          "id": "6",
          "title": "d and f- Block elements",
          "quecount": 42
        }],
        "Physics": [{
          "id": "3",
          "title": "Circular Motion",
          "quecount": 5
        }]
     }
    
     var total = 0;
     angular.forEach($scope.chlist, function(value, key) {
       angular.forEach(value, function(item) {
         total += item.quecount;
        });
     });
    
     $scope.totque = total;
    

    See JSFiddle

    【讨论】:

      【解决方案3】:
      for (i = 0; i < $scope.chlist["Chemistry - II"].length; i++) {  
          total += $scope.chlist["Chemistry - II"][i].quecount;
      } 
      

      尝试添加键-“化学-II”

      【讨论】:

      • @PrashantFepale 更新了代码,为“for”和“total”添加了键
      【解决方案4】:
      var o = { "Chemistry - II": [ { "id": "9", "title": "Solid State", "quecount": 12 }, { "id": "10", "title": "Solutions", "quecount": 9 }, { "id": "11", "title": "Electrochemistry", "quecount": 8 }, { "id": "6", "title": "d and f- Block elements", "quecount": 42 } ], "Physics": [ { "id": "3", "title": "Circular Motion", "quecount": 5 } ] };
      
      var total = 0;
      for(var p in o) {
         if( Array.isArray(o[p]) ) {
            total = o[p].reduce((function(v, item) {
              return v + item.quecount;
            }, total);
         }   
      }
      

      【讨论】:

        【解决方案5】:

        试试这个:

        var myApp = angular.module('myApp',[]);
        
        myApp.controller('MyCtrl',function($scope) {
            
            $scope.jsonObj = {
        	"Chemistry - II": [{
        		"id": "9",
        		"title": "Solid State",
        		"quecount": 12
        	}, {
        		"id": "10",
        		"title": "Solutions",
        		"quecount": 9
        	}, {
        		"id": "11",
        		"title": "Electrochemistry",
        		"quecount": 8
        	}, {
        		"id": "6",
        		"title": "d and f- Block elements",
        		"quecount": 42
        	}],
        	"Physics": [{
        		"id": "3",
        		"title": "Circular Motion",
        		"quecount": 5
        	}]
        };
            var chemistryQuecount = 0;
            var physicsQuecount = 0;
            for (var i in $scope.jsonObj) {
              if(i == 'Chemistry - II') {
                for(var j in $scope.jsonObj[i]) {
                  chemistryQuecount += $scope.jsonObj[i][j].quecount;
                }
              }
              if(i == 'Physics') {
                for(var j in $scope.jsonObj[i]) {
                  physicsQuecount += $scope.jsonObj[i][j].quecount;
                }
              }
            }
            console.log(chemistryQuecount);
            console.log(physicsQuecount);
        });
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
        <div ng-app="myApp" ng-controller="MyCtrl">
        </div>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-01-02
          • 2016-12-08
          • 1970-01-01
          • 1970-01-01
          • 2021-06-13
          • 1970-01-01
          • 2022-06-10
          相关资源
          最近更新 更多