0

The below directive code is forming an JSON dynamically. I have to form a JSON without dupicate object names.

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

Using aboeve code i am getting the JSON. In this JSON inside rowset records are duplicated because of loop. But i'm bit confused on this. I need avoid it. It should create only one time. Anyone please help me to achieve this. If you need more details on this. let me know.

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": []
    }
  }
}
4
  • 1
    add a plnkr/fiddle Commented Apr 7, 2016 at 8:17
  • is it always the same json that is generated? Commented Apr 7, 2016 at 8:40
  • @Durendal yes it's generating same Commented Apr 7, 2016 at 8:55
  • @NitsanBaleli. It;s hard to create plunker because so many things are required. anyhow i will try to create. Thanks... Commented Apr 7, 2016 at 9:54

1 Answer 1

1

I don't know if it's always the same json but you can probably use uniqby from lodash on your rowset array.

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

Demo

EDIT

If you have several customers, you can loop over a customer 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/

Sign up to request clarification or add additional context in comments.

3 Comments

Object(like Customer29Jan16) will come more, its depends on user and each object will have their own attributes and it may come same attribute name for multiple object,
uniqBy method in underscore. i need to use angular only.
It doesn't prevent you using an other library while using angular

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.