0
{ "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 } ] }

I am trying to sum the "quecount" but fail to get the result

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

    $scope.totque = total;

Help will be highly appreciated. Thank you..

1
  • You need total for chemistry and physics separately or total of all ? Commented Feb 22, 2017 at 8:00

5 Answers 5

2

You can use Array.prototype.map() and 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));
});

Sign up to request clarification or add additional context in comments.

Comments

1

Use the angular.forEach to loop over all the groups (like : Chemistry - II), for each group you want to count the quecount.

On your controller:

$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

Comments

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

Try to add key - "Chemistry - II"

1 Comment

@PrashantFepale updated code, added key for both, "for" and "total"
0
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);
   }   
}

Comments

0

Try this :

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.