如何为http.get请求数据制作总计

时间:2017-10-11 07:19:42

标签: javascript jquery css angularjs wordpress

我需要你的专业知识。

我通过http.get请求获取数据,JSON数据如下:

"."

总共有100条记录与这5种信用类型相混合。

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

并且还有每个creditType的积分。

为了查看错误,我通过ng-repeat显示数据,但它运行正常。

任何人都可以帮助我如何获得每种creditType的总数?

对于Ex:creditType:Transfer = ?, creditType:Bought = ?, creditType:Spent = ?,, creditType:Award = ?, creditType:Received =?,

提前致谢!!!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;
                    }
                }
            }