我正在尋找刪除重復代碼的許多行。問題基本上是生成JSON。搜索條件的最后一行具有“ hasValue”屬性或“ value”屬性。它是用TypeScript編寫的,我不能在其中使用{}標記來編寫JavaScript代碼。該代碼按原樣工作,但是95%是重復代碼,因為在hasValue = false的情況下需要使用不同的屬性。有沒有更簡潔的書寫方式?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 } }; }};
簡化用TypeScript編寫此JSON對象的過程
慕妹3146593
2021-04-13 09:14:13