2017-02-22 6 views
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 } ] } 

Я пытаюсь суммировать «quecount», но не в состоянии получить результатSum значение из массива JSON объекта

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

    $scope.totque = total; 

Помощь будет высоко оценен. Спасибо ..

+0

Вам нужно всего по химии и физике в отдельности или в общей сложности все? –

ответ

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

Попробуйте добавить ключ - «Химия - II»

+0

это показать ошибку для моей длины .. :( –

+0

@PrashantFepale обновленный код, добавленный ключ для обоих, для "и" всего " –

0

Используйте angular.forEach, чтобы перебрать все groups (например: химия - 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

2

Вы можете использовать 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)); 
 
});

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); 
    } 
} 
0

Попробуйте это:

var myApp = angular.module('myApp',[]); 
 

 
myApp.controller('MyCtrl',function($scope) { 
 
    
 
    $scope.jsonObj = { 
 
\t "Chemistry - II": [{ 
 
\t \t "id": "9", 
 
\t \t "title": "Solid State", 
 
\t \t "quecount": 12 
 
\t }, { 
 
\t \t "id": "10", 
 
\t \t "title": "Solutions", 
 
\t \t "quecount": 9 
 
\t }, { 
 
\t \t "id": "11", 
 
\t \t "title": "Electrochemistry", 
 
\t \t "quecount": 8 
 
\t }, { 
 
\t \t "id": "6", 
 
\t \t "title": "d and f- Block elements", 
 
\t \t "quecount": 42 
 
\t }], 
 
\t "Physics": [{ 
 
\t \t "id": "3", 
 
\t \t "title": "Circular Motion", 
 
\t \t "quecount": 5 
 
\t }] 
 
}; 
 
    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>