I have a directive code that dynamically forms JSON. The challenge is to create a JSON without duplicate 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;
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;
});
The JSON created by the above code contains duplicated records in the rowset due to looping. I need assistance in preventing this duplication and ensuring each record is only created once. If you require further information on this matter, please 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": []
}
}
}