Necesito su ayuda, tengo un código y necesito optimizarlo, ¿alguien puede saber cómo podría ser esto? Este es mi código:
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; }}
Puede crear un objeto para asignar sus valores de nombre de campo a los nombres de clave de objeto
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; } }Versión del mapa
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; } }