过滤数组中的对象

时间:2018-07-30 06:59:37

标签: javascript arrays loops object

我有一个Promise.All生成的数组结果,它生成2000个以上的对象。我必须根据其bch属性对每个对象进行分类。但是,当我遍历对象并对其进行过滤时,浏览器始终没有响应。我检查了一下,发现内存泄漏。我更改了过滤方式,但得到了一些结果,但问题是其他对象未能基于bch属性进行分类。

这是我的JavaScript

我有一个$scope.parlist = [];,我在那里存储了所有过滤的对象。

for (let res of results[0]) {
    if ($scope.parlist.length < 1) {
       $scope.eachpar = {
            pdaccounts: [],
            raccounts: [],
            bch: "",
            branch: "",
            pdnoofaccts: "",
            pdamount: "",
            rnoofaccts: "",
            ramount: "",
            par: ""
       }
       $scope.eachpar.bch = res.bch;
       if ($scope.eachpar.bch === "001") {
           $scope.eachpar.branch = "AAA";
       } else if ($scope.eachpar.bch === "002") {
           $scope.eachpar.branch = "BBB";
       } else if ($scope.eachpar.bch === "003") {
           $scope.eachpar.branch = "CCC";
       } else if ($scope.eachpar.bch === "004") {
           $scope.eachpar.branch = "DDD";
       }

       $scope.eachpar.pdaccounts.push({ "acct_name": res.acct_name, "acct_no": res.acct_no, "principal": res.principal });
       $scope.eachpar.raccounts.push({ "acct_name": res.acct_name, "acct_no": res.acct_no, "principal": res.principal_bal });
       $scope.eachpar.pdnoofaccts = 1;
       $scope.eachpar.pdamount = res.principal_bal;
       $scope.eachpar.rnoofaccts = 1;
       $scope.eachpar.ramount = res.principal;

       $scope.parlist.push($scope.eachpar);
    }
    else 
    {

         for (var i = 0; i < $scope.parlist.length; i++) {
              setDelay($scope.parlist,$scope.parlist[i], res);
              //here I set delay
         }
     }
}

这是我的setDelay()函数

function setDelay(array,parlisteach,res) {
     setTimeout(function () {
        //I use setTimeout to avoid memory leak
        var obj = array.find(function (obj) { return obj.bch === res.bch; });
        //checking if object with same bch exist in the $scope.parlist
        if (obj !== undefined) {
           if (res.loan_age > 0) {
                parlisteach.pdaccounts.push({ "acct_name": res.acct_name, "acct_no": res.acct_no, "principal": res.principal });
                parlisteach.raccounts.push({ "acct_name": res.acct_name, "acct_no": res.acct_no, "principal": res.principal });
                parlisteach.pdnoofaccts = parlisteach.pdnoofaccts + 1;
                parlisteach.rnoofaccts = parlisteach.rnoofaccts + 1;

                parlisteach.pdamount = parlisteach.pdamount + res.principal_bal;
                parlisteach.ramount = parlisteach.ramount + res.principal;
            } else {
                parlisteach.raccounts.push({ "acct_name": res.acct_name, "acct_no": res.acct_no, "principal": res.principal });
                parlisteach.rnoofaccts = parlisteach.rnoofaccts + 1;
                parlisteach.ramount = parlisteach.ramount + res.principal;
            }
        }
        else
        { //if not exist in $scope.parlist, add it
            $scope.eachpar = {
                pdaccounts: [],
                raccounts: [],
                bch: "",
                branch: "",
                pdnoofaccts: "",
                pdamount: "",
                rnoofaccts: "",
                ramount: "",
                par: ""
            }

            $scope.eachpar.bch = res.bch;
            if ($scope.eachpar.bch === "001") {
                $scope.eachpar.branch = "AAA";
            } else if ($scope.eachpar.bch === "002") {
                $scope.eachpar.branch = "BBB";
            } else if ($scope.eachpar.bch === "003") {
                $scope.eachpar.branch = "CCC";
            }else if ($scope.eachpar.bch === "004") {
                $scope.eachpar.branch = "DDD";
            }

            $scope.eachpar.pdaccounts.push({ "acct_name": res.acct_name, "acct_no": res.acct_no, "principal": res.principal });
            $scope.eachpar.raccounts.push({ "acct_name": res.acct_name, "acct_no": res.acct_no, "principal": res.principal_bal });
            $scope.eachpar.pdnoofaccts = 1;
            $scope.eachpar.pdamount = res.principal_bal;
            $scope.eachpar.rnoofaccts = 1;
            $scope.eachpar.ramount = res.principal;

            array.push($scope.eachpar);

        }

     });
}

这是我想要的结果

$scope.parlist = [
    {
        pdaccounts:[{"acct_name":"Test","acct_no":"105451","principal":2000},..],
        raccounts: [{"acct_name":"Test","acct_no":"105451","principal":2000},..],
        "bch": "001",
        "branch": "AAA",
        "pdnoofaccts": 100
        ...
    },
    {
        pdaccounts:[{"acct_name":"Test","acct_no":"105451","principal":2000},..],
        raccounts: [{"acct_name":"Test","acct_no":"105451","principal":2000},..],
        "bch": "002",
        "branch": "BBB",
        "pdnoofaccts": 50
        ...
    },
]

我使用当前代码实现了此问题,但是问题是所有res对象都以{001“ bch转到$scope.parlist中的对象,而其他$scope.parlist对象是“ 002”只有一个项目。我不知道错误出在我当前的代码中。请帮助我。

0 个答案:

没有答案