2015-09-08 73 views
-2

JSON數據,從Angular JS中的嵌套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

環路和添加,有喲你試過什麼? – tymeJV

+0

是的,我已經嘗試循環,但我無法顯示我的HTML中的數據。 –

+0

發佈您到目前爲止嘗試過的方法,以及哪些方法無效。 – tymeJV

回答

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/