2017-10-11 60 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": ""} 

在總的I有100條記錄包含有這些5個creditTypes混合。

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

並且還有每個creditType的積分。

爲了看到我用ng-repeat顯示數據的錯誤,但它工作的很完美。

任何人都可以幫助我如何獲得每個creditType的總數?

對於防爆:creditType:傳輸=,creditType:買=,creditType:花=, creditType:獎=,creditType:??預收=,

謝謝!

+0

你想要基於信用類型的「信用」總數嗎? –

+0

是的是Surjeet Bhadauriya先生。你能幫我解決嗎? –

回答

0

使用組類似

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"); 

現在,你可以指望每個組使用this link for sum

working fiddle

+0

謝謝Jitender先生,我需要基於creditType的信用總額。每個creditType都有信用數量。你能幫助我嗎? –

+0

檢查更新小提琴 – 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; 
        } 
       } 
      }