来自Angular JS中嵌套JSON数据的总和?

时间:2015-09-08 18:02:09

标签: json angularjs

JSON数据,

[
  {
    "id":1,
    "empnumber":"sada",
    "photo":"assets/images/59.jpg",
    "name":"dummy Nagda",
    "designation":" Executive Officer",
    "dateofjoining":"15/05/2002",
    "location":"India",
    "profileDescription":"Lorem ipsum....",
    "awards":[
        {
          "category": "Monthly ",
          "points": [
            { "name": "First Appreciation", "score": 50 , "Reason":"Lorem ipsum..."},
            { "name": "Second Appreciation", "score": 50 , "Reason":"Lorem ipsum..." },
            { "name": "Third Appreciation", "score": 50 , "Reason":"Lorem ipsum..." },
            { "name": "Fourth Appreciation", "score": 50 , "Reason":"Lorem ipsum..." },
            { "name": "Fifth Appreciation", "score": 50 , "Reason":"Lorem ipsum..." },
            { "name": "Sixth Appreciation", "score": 50 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Reward",
          "points": [
            { "name": "Work of Arrk Award", "score": 79 , "Reason":"Lorem ipsum..." },
            { "name": "Masterpiece Award", "score": 84 , "Reason":"Lorem ipsum..." },
            { "name": "Arrk Ambassador Award", "score": 84 , "Reason":"Lorem ipsum..." },
            { "name": "Ace Tech Arrkitect Award", "score": 84 , "Reason":"Lorem ipsum..." },
            { "name": "Star of Support Award", "score": 84 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Training",
          "points": [
            { "name": "Self enhancement", "score": 97 , "Reason":"Lorem ipsum..." },
            { "name": "By Management", "score": 93 , "Reason":"Lorem ipsum..." },
            { "name": "Project Requirement", "score": 93 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Self Initiative",
          "points": [
            { "name": "Conducting Training", "score": 97 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Employee Engagment",
          "points": [
            { "name": "any", "score": 97 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Other",
          "points": [
            { "name": "any", "score": 97 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Active Participation in Meetings",
          "points": [
            { "name": "any", "score": 97 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Helpful nature in work related areas",
          "points": [
            { "name": "any", "score": 97 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Active Helping in recruitment",
          "points": [
            { "name": "any", "score": 97 , "Reason":"Lorem ipsum..." }
          ] 
        }
    ]
  },
  {
    "id":2,
    "empnumber":"sa",
    "photo":"assets/images/44.jpg",
    "name":"aa Zalkikar",
    "designation":"Vice President(Delivery Management)",
    "dateofjoining":"12/4/2004",
    "location":"India",
    "profileDescription":"Lorem ipsum....",
    "awards":[
        {
          "category": "Monthly ",
          "points": [
            { "name": "First Appreciation", "score": 50 , "Reason":"Lorem ipsum..."},
            { "name": "Second Appreciation", "score": 50 , "Reason":"Lorem ipsum..." },
            { "name": "Third Appreciation", "score": 50 , "Reason":"Lorem ipsum..." },
            { "name": "Fourth Appreciation", "score": 50 , "Reason":"Lorem ipsum..." },
            { "name": "Fifth Appreciation", "score": 50 , "Reason":"Lorem ipsum..." },
            { "name": "Sixth Appreciation", "score": 50 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Reward",
          "points": [
            { "name": "Work of Arrk Award", "score": 79 , "Reason":"Lorem ipsum..." },
            { "name": "Masterpiece Award", "score": 84 , "Reason":"Lorem ipsum..." },
            { "name": "Arrk Ambassador Award", "score": 84 , "Reason":"Lorem ipsum..." },
            { "name": "Ace Tech Arrkitect Award", "score": 84 , "Reason":"Lorem ipsum..." },
            { "name": "Star of Support Award", "score": 84 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Training",
          "points": [
            { "name": "Self enhancement", "score": 97 , "Reason":"Lorem ipsum..." },
            { "name": "By Management", "score": 93 , "Reason":"Lorem ipsum..." },
            { "name": "Project Requirement", "score": 93 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Self Initiative",
          "points": [
            { "name": "Conducting Training", "score": 97 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Employee Engagment",
          "points": [
            { "name": "any", "score": 97 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Other",
          "points": [
            { "name": "any", "score": 97 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Active Participation in Meetings",
          "points": [
            { "name": "any", "score": 97 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Helpful nature in work related areas",
          "points": [
            { "name": "any", "score": 97 , "Reason":"Lorem ipsum..." }
          ] 
        },
        {
          "category": "Active Helping in recruitment",
          "points": [
            { "name": "any", "score": 97 , "Reason":"Lorem ipsum..." }
          ] 
        }
    ]
  }
]

如何计算json数据中所有得分的总和?

我的app.js看起来像这样,我已经迭代了json数据以获得总分并试图在视图上使用$ scope.sum变量

var app = angular.module('myApp', []);
app.controller('LeaderboardController', function($scope, $http) {
$http.get("gamify.json")
.success(function (response) {
    $scope.names = response;
        angular.forEach($scope.names, function(name, mainIndex) {
           angular.forEach(name.awards,function(awards,maindemoIndex){ 
            angular.forEach(awards.points,function(point,baseIndex){
                   $scope.sum+=point.score     
            });
        });
    });     

}); });

这是我到目前为止所尝试过的。

1 个答案:

答案 0 :(得分:0)

您可以使用.map.reduce的组合来循环并总计得分,然后分配到$scope个变量(尽管标准for循环可能更容易读取):

$scope.totalScore = data.map(function(item) {
     return item.awards.map(function(award) {
          //Map out the score from each inner score array
          return award.points.map(function(score) {
               return score.score;
          //Total those scores
          }).reduce(function(p, c) {
               return p + c;
          });
      //Total all inner arrays
      }).reduce(function(p, c) {
          return p + c;
      });
      //Total everything!
 }).reduce(function(p, c) {
     return p + c;
 });

常规for循环:

var total = 0;
for (var i = 0; i < data.length; i++) {
    for (var j = 0; j < data[i].awards.length; j++) {
        for (var k = 0; k < data[i].awards[j].points.length; k++) {
            total += data[i].awards[j].points[k].score
        }
    }
}

演示:http://jsfiddle.net/jupfn747/3/