如何避免重复记录为每个循环添加内部角度

How to avoid duplicate records adding inside angular for each loop?

本文关键字:添加 循环 内部 何避免 记录      更新时间:2024-02-08

下面的指令代码正在动态地形成一个JSON。我必须形成一个没有重复对象名称的JSON。

角度代码:

bosAppModule.directive('layoutTableCellControlLabelRender',function($compile,$rootScope){
    var layoutTableCellControlLabelObj={};
    var soJSON = {
            entityInfo: {
                entity: "",
                tenantId: "",
                timeStamp: new Date().toJSON().toString()
            },
            collections: {
            }
    };
    var myobj={};
    linkFnTableCellControlLabel=function(scope, element, attributes, controllerCtrl) {
        scope.labelData="NO DATA";
        angular.forEach(scope.pageObject.collections.objectattribute.rowset, function (value, index) {
            if(value.objectattributeid==scope.attributeId){
                scope.labelData=value.objectattributelabelname;
                scope.attributeName=value.objectattributename;
                //$rootScope.$broadcast("NEW_EVENT", scope.attributeName);              
                angular.forEach(scope.pageObject.collections.object.rowset, function (value2, index2) {
                    if(value2.tenantobjectid==value.objectattributeobjectid){
                        scope.objectname=value2.tenantobjectname;
                        if(!soJSON.collections[scope.objectname]) {
                            soJSON.collections[scope.objectname]={
                            "meta": {
                                "parentreference": "***",
                                "pkname": "***",
                                "fkname": "***"
                              },
                              "rowset": [],
                              "rowfilter": []
                             };
                        }
                    }
                });
                myobj[scope.attributeName]="test";
                soJSON.collections[scope.objectname].rowset.push(myobj);
            }
        });
        console.log(JSON.stringify(soJSON));
    };

    layoutTableCellControlLabelObj.scope={attributeId:'=',layoutData:'=',pageObject:'='};
    layoutTableCellControlLabelObj.restrict='AE';
    layoutTableCellControlLabelObj.replace='true';
    layoutTableCellControlLabelObj.template="<div class='col-xs-12 col-sm-12 col-md-6 col-lg-6' attribute-name={{attributeName}} attribute-id='tablecellcontrol.layouttablecellcontrolbindingobjectattributeid' " +
                                            "layout-data='layoutData' page-object='pageObject'><label class='k-label pull-right'>{{labelData}}</label></div>";
    layoutTableCellControlLabelObj.link = linkFnTableCellControlLabel;
    return layoutTableCellControlLabelObj;  
});

使用上面的代码,我得到了JSON。在这个JSON中,行集内的记录由于循环而重复。但我对此有点困惑。我需要避免它。它应该只创建一次。任何人都请帮助我实现这一目标。如果你需要更多的细节。让我知道。

JSON-

 {
  "entityInfo": {
    "entity": "",
    "tenantId": "",
    "timeStamp": "2016-04-07T07:25:49.711Z"
  },
  "collections": {
    "Customer29Jan16": {
      "meta": {
        "parentreference": "***",
        "pkname": "***",
        "fkname": "***"
      },
      "rowset": [
        {
          "CuId": "test",
          "Name": "test",
          "Quantity": "test",
          "Rate": "test",
          "Amount": "test"
        },
        {
          "CuId": "test",
          "Name": "test",
          "Quantity": "test",
          "Rate": "test",
          "Amount": "test"
        },
        {
          "CuId": "test",
          "Name": "test",
          "Quantity": "test",
          "Rate": "test",
          "Amount": "test"
        },
        {
          "CuId": "test",
          "Name": "test",
          "Quantity": "test",
          "Rate": "test",
          "Amount": "test"
        },
        {
          "CuId": "test",
          "Name": "test",
          "Quantity": "test",
          "Rate": "test",
          "Amount": "test"
        }
      ],
      "rowfilter": []
    }
  }
}

我不知道它是否总是相同的json,但您可能可以在rowset数组上使用来自lodash的uniqby。

_.uniqBy(json.collections.Customer29Jan16.rowset, function (e) {
   return e.CuId;
});

演示

编辑

如果你有几个客户,你可以在一个客户阵列上循环:

arrayOfCustomers = (Object.keys(json.collections));
for(var i = 0 ; i < arrayOfCustomers.length; i++){
  json.collections[arrayOfCustomers[i]].rowset  = _.uniqBy(json.collections[arrayOfCustomers[i]].rowset, function (e) {
    return e.CuId;
  });
}

https://jsfiddle.net/kq9gtdr0/23/