我有這段代碼,我想將其重構為更好的解決方案:
async onServerRequestForDraft(res: { value: {}; tabType: ETabType; }) {
res.value = this.uiForm.value;
res.value = this.data;
res.value['brokerage'] = this.uiForm.value.brokerage != null ? this.uiForm.value.brokerage : { id: 0, name: null };
res.value['org'] = this.uiForm.value.org;
res.value['selectedPhone'] = this.uiForm.value.selectedPhone || '';
res.value['customerName'] = this.uiForm.value.customerName || '';
}
我不能使用 forEach 因為 res 不是陣列。也許另一種解決方案?
uj5u.com熱心網友回復:
盡管您可以使用它,但您不需要 forEach 即可。
async onServerRequestForDraft(res: { value: {}, tabType: ETabType }) {
Object.assign(res.value, {
'brokerage': this.uiForm.value.brokerage ?? { id: 0, name: null },
'org': this.uiForm.value.selectedPhone ?? '',
'customerName': this.uiForm.value.customerName ?? '';
})
return res;
}
如果你只想使用回圈,你可以這樣做:
async onServerRequestForDraft(res: { value: {}, tabType: ETabType }) {
for (const key in this.uiForm.value) {
if (key === 'brokerage') {
Object.assign(res.value, {key : this.uiForm.value[key] ?? { id: 0, name: null }});
continue;
}
Object.assign(res.value, {key : this.uiForm.value[key] ?? ''});
}
return res;
}
uj5u.com熱心網友回復:
你可以像這樣在 Object 上使用 forEach :
Object.entries(this.uiForm.value).forEach((item) => {
// item[0] object key
// item[1] object value
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/470959.html