let column_head_order_no, column_old_order_code, column_customer_name, column_head_memo,column_order_date, column_sum_tax_amount,column_creator,column_customer_tax_id,column_customer_address, column_customer_tel,column_customer_bank,column_customer_bank_account,column_customer_email,column_org_name advanceConfig.value.apiKingdeeDetailTableSetting.mainTableField.forEach(item => { if (item.name === 'order_no') { column_head_order_no = item.field_name } if (item.name === 'old_order_code') { column_old_order_code = item.field_name } if (item.name === 'customer_name') { column_customer_name = item.field_name } if (item.name === 'memo') { column_head_memo = item.field_name } if (item.name === 'order_date') { column_order_date = item.field_name } if (item.name === 'sum_tax_amount') { column_sum_tax_amount = item.field_name } if (item.name === 'sum_tax_amount') { column_sum_tax_amount = item.field_name } }) 优化这段代码
时间: 2024-03-07 18:49:01 浏览: 49
可以使用对象字面量和数组的解构来简化这段代码:
```
const fieldMap = {
order_no: 'column_head_order_no',
old_order_code: 'column_old_order_code',
customer_name: 'column_customer_name',
memo: 'column_head_memo',
order_date: 'column_order_date',
sum_tax_amount: 'column_sum_tax_amount',
creator: 'column_creator',
customer_tax_id: 'column_customer_tax_id',
customer_address: 'column_customer_address',
customer_tel: 'column_customer_tel',
customer_bank: 'column_customer_bank',
customer_bank_account: 'column_customer_bank_account',
customer_email: 'column_customer_email',
org_name: 'column_org_name'
}
const columns = {}
advanceConfig.value.apiKingdeeDetailTableSetting.mainTableField.forEach(item => {
if (fieldMap[item.name]) {
columns[fieldMap[item.name]] = item.field_name
}
})
```
这样可以去除大量的重复代码,同时也更加易读和易于维护。
阅读全文