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

时间:2016-04-07 07:49:27

标签: javascript angularjs json

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

Angular Code:

 Dim ws As Worksheet
 Set ws = Sheets("Data")
 ws.Visible = xlSheetVisible
 ws.Select
 With ws
 .Columns("O:O").Select
 End With

使用aboeve代码我得到了JSON。在这个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;  
});

1 个答案:

答案 0 :(得分:1)

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

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

Demo

修改

如果您有多个客户,则可以循环客户数组:

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/