gpt4 book ai didi

javascript - 如何避免为每个循环添加内部 Angular 重复记录?

转载 作者:行者123 更新时间:2023-12-03 07:14:57 28 4
gpt4 key购买 nike

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

Angular 代码:

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

使用上面的代码我得到了 JSON。在此 JSON 中,行集内的记录由于循环而重复。但我对此有点困惑。我需要避免它。它应该只创建一次。 任何人请帮助我实现这一目标。如果您需要更多详细信息。让我知道。

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

最佳答案

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

_.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/

关于javascript - 如何避免为每个循环添加内部 Angular 重复记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36469873/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com