Reputation: 577
{ "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..
Upvotes: 0
Views: 2093
Reputation: 27192
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>
Upvotes: 0
Reputation: 4319
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);
}
}
Upvotes: 0
Reputation: 418
for (i = 0; i < $scope.chlist["Chemistry - II"].length; i++) {
total += $scope.chlist["Chemistry - II"][i].quecount;
}
Try to add key - "Chemistry - II"
Upvotes: 0
Reputation: 13943
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));
});
Upvotes: 2
Reputation: 3820
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;
Upvotes: 1