简体   繁体   中英

How to make total for http.get request data

I need your expertise on this.

I am getting data through http.get request and the JSON data is like follows:

{"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": ""}

In total I have 100 records mixed with these 5 creditTypes.

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

and there are also credits for each creditType.

In order to see the bug I am displaying the data by ng-repeat but it is working perfectly.

Anyone can help me how can I get the total for each creditType?

For Ex: creditType:Transfer = ?, creditType:Bought = ?, creditType:Spent = ?, creditType:Award = ?, creditType:Received = ?,

Thanks in advance!!!

Use group something like

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

Now you can count for each group use this link for sum

working fiddle

Looks Jiterder has given a very good answer. You may calculate this by writing a simple function as well. It will loop through the data and calculate the sum for the different groups.

$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;
                    }
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM