2017-10-11 1 views
-1

이것에 대한 전문 지식이 필요합니다.http.get 요청 데이터를 합산하는 방법

나는 http.get 요청을 통해 데이터를 얻고 다음과 같은 JSON 데이터는 다음과 같습니다 합계에서

{"displayTransactionDateTime": "Tue, 19th Sep 2017" 
, "transactionDateAndTime": "2017-09-19 18:36:34.0" 
, "transactionId": "158131" 
, "refTransactionId": null 
, "transactionType": "Credit Transfer" 
, "creditType": "Transfer" 
, "transactionMode": "SYSTEM CREDITS" 
, "transactionCurrency": "SYSTEM CREDITS" 
, "transactionAmount": "50" 
, "credits": "50" 
, "creditMessage": ""}, 
{"displayTransactionDateTime": "Wed, 13th Sep 2017" 
, "transactionDateAndTime": "2017-09-13 09:53:28.0" 
, "transactionId": "157687" 
, "refTransactionId": null 
, "transactionType": "Credit Purchase" 
, "creditType": "Bought" 
, "transactionMode": "CREDIT CARD" 
, "transactionCurrency": "AED" 
, "transactionAmount": "50" 
, "credits": "10" 
, "creditMessage": null}, 
{"displayTransactionDateTime": "Sat, 9th Sep 2017" 
, "transactionDateAndTime": "2017-09-09 14:49:42.0" 
, "transactionId": "157378" 
, "refTransactionId": null 
, "transactionType": "Voucher Purchase" 
, "creditType": "Spent" 
, "transactionMode": "SYSTEM CREDITS" 
, "transactionCurrency": "SYSTEM CREDITS" 
, "transactionAmount": "5" 
, "credits": "5" 
, "creditMessage": null} 
{ 
"displayTransactionDateTime": "Tue, 7th Feb 2017" 
, "transactionDateAndTime": "2017-02-07 19:11:40.0" 
, "transactionId": "34133" 
, "refTransactionId": "34132" 
, "transactionType": "Credit Award" 
, "creditType": "Award" 
, "transactionMode": "SYSTEM CREDITS" 
, "transactionCurrency": "SYSTEM CREDITS" 
, "transactionAmount": "1" 
, "credits": "1" 
, "creditMessage": "Referral Credit Award"} 
{"displayTransactionDateTime": "Sat, 20th Aug 2016" 
, "transactionDateAndTime": "2016-08-20 17:50:42.0" 
, "transactionId": "10348" 
, "refTransactionId": "10347" 
, "transactionType": "Credit Received" 
, "creditType": "Recieved" 
, "transactionMode": "SYSTEM CREDITS" 
, "transactionCurrency": "SYSTEM CREDITS" 
, "transactionAmount": "1" 
, "credits": "1" 
, "creditMessage": ""} 

나는이 5 creditTypes와 혼합 (100 개) 기록을 가지고있다.

"creditType": "Transfer", "creditType": "Bought", "creditType": "Spent", 
"creditType": "Award", "creditType": "Received". 

및 각 신용 유형별 크레딧도 있습니다.

버그를 보려면 ng-repeat로 데이터를 표시하고 있지만 완벽하게 작동하고 있습니다.

누구나 어떻게하면 각 신용 유형의 총계를 얻을 수 있습니까? 예를 들어

: creditType : 전송 = creditType : 구입 = creditType : 지출 = creditType : 상 = creditType :? 사전에 =

감사 수상!

function groupBy(arr, key) { 
     var newArr = [], 
      types = {}, 
      newItem, i, j, cur; 
     for (i = 0, j = arr.length; i < j; i++) { 
      cur = arr[i]; 
      if (!(cur[key] in types)) { 
       types[cur[key]] = { type: cur[key], data: [] }; 
       newArr.push(types[cur[key]]); 
      } 
      types[cur[key]].data.push(cur); 
     } 
     return newArr; 
}; 

$scope.groupedItems=groupBy(data,"creditType"); 

같은

+0

신용 유형별로 "크레딧"합계를 원합니까? –

+0

예 Mr. Surjeet Bhadauriya. 이걸 좀 도와 주실 래요 ?? –

답변

0

사용 그룹 뭔가 이제 this link for sum

working fiddle

+0

Mr. Jitender, 신용 유형에 따라 신용 합계가 필요합니다. 각 creditType은 크레딧 수를가집니다. pls 날이 도울 수 있나요 ??? –

+0

check updated fiddle – jitender

+0

정말 고마워요 !!!!! 너 바위 야. –

0

이 Jiterder 아주 좋은 답변을 주신 보이는 각 그룹 사용하기 위해 셀 수 있습니다. 간단한 함수를 작성하여 이것을 계산할 수도 있습니다. 그것은 데이터를 통해 반복하고 다른 그룹에 대한 합계를 계산합니다.

$scope.getData = function(){ 
       for (var i = 0; i < $scope.data.length; i++){ 
        switch ($scope.data[i].creditType){ 
         case 'Transfer': $scope.transfer += $scope.data[i].credits;break; 
         case 'Bought': $scope.bought += $scope.data[i].credits;break; 
         case 'Spent': $scope.spent += $scope.data[i].credits;break; 
         case 'Award': $scope.award += $scope.data[i].credits;break; 
         case 'Received': $scope.received += $scope.data[i].credits;break; 
        } 
       } 
      } 
관련 문제