我有一个重复使用的打字稿代码。我想通过将代码分配给变量并使用它们来重用代码。例如,我在map函数中定义了一个箭头函数以返回一个对象,并且我想知道重用该对象的最佳方法。
完整的部分代码:
let options: kendo.ui.GridOptions = {
dataSource: {
pageSize: 10,
serverFiltering: true,
transport: {
read: (opts: kendo.data.DataSourceTransportReadOptions) => {
self.http.get("/api/AssetConfiguration").then((response) => {
let resources: any;
let activeAssetTab = response.data.filter(x => (x.active == true && x.assetTypeActive == true && x.assetSubTypeActive == true));
let inActiveAssetTab = response.data.filter(x => (x.active == true || x.assetTypeActive == true || x.assetSubTypeActive == true));
let selectedTab = active ? activeAssetTab : inActiveAssetTab;
let mappedObjects = (x) => {
assetClassificationId: x.assetClassificationId,
assetTypeName: x.assetTypeName,
assetSubTypeName: x.assetSubTypeName,
assetClassificationName: x.assetClassificationName,
classificationHiddenFromUser: x.classificationHiddenFromUser
};
if (self.searchQuery) {
if (active) {
resources = activeAssetTab.filter(x =>
((x.searchText.toLowerCase().indexOf(self.searchQuery.toLowerCase()) > -1) == true))
.map(x => {
return {
assetClassificationId: x.assetClassificationId,
assetTypeName: x.assetTypeName,
assetSubTypeName: x.assetSubTypeName,
assetClassificationName: x.assetClassificationName,
classificationHiddenFromUser: x.classificationHiddenFromUser
}
});
} else {
resources = inActiveAssetTab.filter(x =>
((x.searchText.toLowerCase().indexOf(self.searchQuery.toLowerCase()) > -1) == true))
.map(x => {
return {
assetClassificationId: x.assetClassificationId,
assetTypeName: x.assetTypeName,
assetSubTypeName: x.assetSubTypeName,
assetClassificationName: x.assetClassificationName,
classificationHiddenFromUser: x.classificationHiddenFromUser
}
});
}
} else {
resources = selectedTab
.map(x => {
return {
assetClassificationId: x.assetClassificationId,
assetTypeName: x.assetTypeName,
assetSubTypeName: x.assetSubTypeName,
assetClassificationName: x.assetClassificationName,
classificationHiddenFromUser: x.classificationHiddenFromUser
};
});
}
opts.success(resources);
});
}
}
},
columns: [
{ field: 'assetTypeName', title: 'Asset Type', template: '<span ng-click="$ctrl.edit(#: assetClassificationId#);">#: assetTypeName #</span>' },
{ field: 'assetSubTypeName', title: 'Asset Sub Type' },
{ field: 'assetClassificationName', title: 'Asset Classification', template: '<span ng-if="#: classificationHiddenFromUser #">n/a</span><span ng-if="!#: classificationHiddenFromUser #">#: assetClassificationName #</span>' },
{ field: 'assetClassificationId', title: 'Edit', template: '<a class="k-button k-button-icontext k-grid-edit" ng-click="$ctrl.edit(#: assetClassificationId#);"><span class="k-icon k-edit"></span>Edit</a>', width: "100px" }
]
}
我要优化的代码是:
.map(x => {
return {
assetClassificationId: x.assetClassificationId,
assetTypeName: x.assetTypeName,
assetSubTypeName: x.assetSubTypeName,
assetClassificationName: x.assetClassificationName,
classificationHiddenFromUser: x.classificationHiddenFromUser
}
})
如何在map函数中重用同一对象?
答案 0 :(得分:3)
只需创建一个返回对象并将其引用传递给.map
的函数。参见下面的示例。
const myFunction = x => {
return {
assetClassificationId: x.assetClassificationId,
assetTypeName: x.assetTypeName,
assetSubTypeName: x.assetSubTypeName,
assetClassificationName: x.assetClassificationName,
classificationHiddenFromUser: x.classificationHiddenFromUser
};
}
resources = activeAssetTab.filter(x =>
((x.searchText.toLowerCase().indexOf(self.searchQuery.toLowerCase()) > -1) == true))
.map(myFunction);