[英]How can I best optimize my angular function?
I need your help, I have a code and I need to optimize it, can anyone know how this could be?我需要你的帮助,我有一个代码,我需要优化它,有人知道这是怎么回事吗? This is my code:
这是我的代码:
handlerChangeInfo(value, fieldName: string) {
if (fieldName === 'fullName') {
this.billingFields.full_name = value;
}
if (fieldName === 'address') {
this.billingFields.address = value;
}
if (fieldName === 'postalCode') {
this.billingFields.postal_code = value;
}
if (fieldName === 'city') {
this.billingFields.city = value;
}
if (fieldName === 'stateOrProvince') {
this.billingFields.state_or_province = value;
}
if (fieldName === 'taxId') {
this.billingFields.tax_id = value;
}
} }
You can create an object to map your field name values to the object key names您可以创建 object 到 map 您的字段名称值到 object 键名称
const obj = {
fullName: 'full_name',
address: 'address',
postalCode: 'postal_code',
city: 'city',
stateOrProvince: 'state_or_province',
taxId: 'tax_id'
}
handlerChangeInfo(value, fieldName: string) {
if (obj[fieldName]) {
this.billingFields[obj[fieldName]] = value;
}
}
Map version Map版
const obj = new Map([
['fullName', 'full_name'],
['address', 'address'],
['postalCode', 'postal_code'],
['city', 'city'],
['stateOrProvince', 'state_or_province'],
['taxId', 'tax_id']
]);
handlerChangeInfo(value, fieldName: string) {
if (obj.get(fieldName)) {
this.billingFields[obj.get(fieldName)] = value;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.