我正在尋找刪除重復(fù)代碼的許多行。問(wèn)題基本上是生成JSON。搜索條件的最后一行具有“ hasValue”屬性或“ value”屬性。它是用TypeScript編寫(xiě)的,我不能在其中使用{}標(biāo)記來(lái)編寫(xiě)JavaScript代碼。該代碼按原樣工作,但是95%是重復(fù)代碼,因?yàn)樵趆asValue = false的情況下需要使用不同的屬性。有沒(méi)有更簡(jiǎn)潔的書(shū)寫(xiě)方式?export const valueSearch = (value: string, hasEmptyValue: boolean, profile: ProfileHolder, attributeId: string, objectTypeId: string, typeName: string = "ValueSearch") => { if (!hasValue) { return { "__typename": "SearchFromProfile", "profileHolder": { "__typename": profile.__typename, "Id": profile.Id }, "search": { "__typename": "HasValue", "objectTypeId": objectTypeId, "attributeId": attributeId, "hasValue": false } }; } else { return { "__typename": "SearchFromProfile", "profileHolder": { "__typename": profile.__typename, "Id": profile.Id }, "search": { "__typename": typeName, "objectTypeId": objectTypeId, "attributeId": attributeId, "value": value } }; }};
簡(jiǎn)化用TypeScript編寫(xiě)此JSON對(duì)象的過(guò)程
慕妹3146593
2021-04-13 09:14:13