2016-04-07 7 views
0

Der folgende Anweisungscode bildet einen JSON dynamisch. Ich muss ein JSON ohne doppelte Objektnamen bilden.Wie vermeidet man doppelte Datensätze, die für jede Schleife Innenwinkel hinzufügen?

Angular Code:

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; 
}); 

aboeve Code ich mit dem JSON bin immer. In diesem JSON-internen Rowset werden Datensätze aufgrund von Schleifen dupliziert. Aber ich bin etwas verwirrt. Ich muss es vermeiden. Es sollte nur einmal erstellt werden. Bitte helfen Sie mir, dies zu erreichen. Wenn Sie mehr Details dazu benötigen. Gib mir Bescheid.

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": [] 
    } 
    } 
} 
+1

eine plnkr/Geige hinzufügen –

+0

ist es immer der gleiche json, der generiert wird? – AshBringer

+0

@Durendal Ja, es erzeugt dasselbe – bagya

Antwort

1

Ich weiß nicht, ob es immer die gleiche json ist aber wahrscheinlich uniqby from lodash auf Ihrem rowset Array verwenden können.

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

Demo

EDIT

Wenn Sie mehrere Kunden haben, können Sie eine Schleife über einen Kunden-Array:

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/

+0

Objekt (wie Customer29Jan16) wird mehr kommen, es hängt vom Benutzer ab und jedes Objekt wird ihre eigenen Attribute haben und es kann den gleichen Attributnamen für mehrere Objekte, – bagya

+0

uniqBy-Methode in Unterstrich kommen. Ich muss nur Winkel verwenden. – bagya

+0

Es verhindert nicht, dass Sie eine andere Bibliothek verwenden, während Sie eckig verwenden – AshBringer