【问题标题】:How to make total for http.get request data如何计算 http.get 请求数据的总数
【发布时间】:2017-10-11 07:19:42
【问题描述】:

我需要你的专业知识。

我通过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": ""}

我总共有 100 条记录与这 5 个 creditTypes 混合。

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

每个 creditType 也有学分。

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

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

例如:creditType:Transfer = ?, creditType:Bought = ?, creditType:Spent = ?, creditType:Award = ?, creditType:Received = ?,

提前致谢!!!

【问题讨论】:

  • 您想要基于信用类型的“信用”总数吗?
  • 是的,Surjeet Bhadauriya 先生。你能帮我解决这个问题吗?

标签: javascript jquery css angularjs wordpress


【解决方案1】:

使用类似的组

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

【讨论】:

  • 谢谢 Jitender 先生,我需要基于 creditType 的信用总额。每个 creditType 都有积分数。你能帮我吗???
【解决方案2】:

看起来 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;
                    }
                }
            }

【讨论】:

    猜你喜欢
    • 2016-05-10
    • 2022-06-16
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    相关资源
    最近更新 更多