Reputation: 399
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": []
}
}
}
Upvotes: 0
Views: 973
Reputation: 2673
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;
});
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/
Upvotes: 1