switch (key) { case 'field': conditionName = `${fieldLabel ?? ''}${conditionType ?? ''}${form.value ?? ''}`; break; case 'op': if (value === ConditionTypeEnum.IS_NULL || value === ConditionTypeEnum.IS_NOT_NULL) { // 为空和非空时,清空条件值 conditionName = `${fieldLabel ?? ''}${conditionType ?? ''}`; } else { conditionName = `${fieldLabel ?? ''}${conditionType ?? ''}${form.value ?? ''}`; } break; case 'value': conditionName = `${fieldLabel ?? ''}${conditionType ?? ''}${form.value ?? ''}`; }简化代码
时间: 2024-01-11 09:02:42 浏览: 21
可以将代码简化如下:
```
conditionName = `${fieldLabel ?? ''}${conditionType ?? ''}`;
if (key === 'field' || key === 'value') {
conditionName += form.value ?? '';
}
if (key === 'op' && (value === ConditionTypeEnum.IS_NULL || value === ConditionTypeEnum.IS_NOT_NULL)) {
conditionName = conditionName.slice(0, -form.value.length);
}
```
这样可以将 `key` 和 `value` 分别处理,避免了多个 `case` 中重复的代码。同时,对于 `IS_NULL` 和 `IS_NOT_NULL` 的情况,可以使用字符串切片来删除不必要的条件值。
相关问题
// 获取表单值 const form = getFieldsValue(); // 条件值的component类型会随着字段的变化发生变化 // 条件值发生变化时获取当前字段列详细数据 const currentFieldData = columnOptions.value.find( (item: Recordable) => item.value === form.field, ) as unknown as Recordable; // 根据字段label const fieldLabel = columnOptions.value.find((item) => item.value === form?.field)?.label; // 判断条件 const conditionType = ARITHMETIC_OPERATOR_BACK.find( (item) => item.value === form?.op, )?.operate; if (form.op === ConditionTypeEnum.IS_NULL || form.op === ConditionTypeEnum.IS_NOT_NULL) { // 判断条件为空、非空时,清空条件值 conditionName = ${fieldLabel ?? ''}${conditionType ?? ''}; } else { // 字段值和判断条件发生变化 let labelText; // 字段变化 if (key === 'field') { if (currentFieldData?.component !== FormElementEnum.SWITCH) { // 清空条件值 await setFieldsValue({ value: undefined }); labelText = ''; } else { await setFieldsValue({ value: 0 }); labelText = '否'; } } else { // 判断条件和条件值变化 // 当前字段有值集时,component类型为ApiSelect if (currentFieldData?.component === FormElementEnum.API_SELECT) { const fieldVsList = await getVsList(currentFieldData.vsId); labelText = fieldVsList?.find( (item: Recordable) => item.itemValue === form.value, )?.itemText; } else if (currentFieldData?.component === FormElementEnum.SWITCH) { // 当前字段类型为boolean时,component类型为Switch,条件值为开关 labelText = form.value === 1 ? '是' : '否'; } else { // 其他 labelText = form.value; } } conditionName = ${fieldLabel ?? ''}${conditionType ?? ''}${labelText ?? ''}; }简化if语句
可以将 if-else 语句简化为三目运算符,如下所示:
```
conditionName = form.op === ConditionTypeEnum.IS_NULL || form.op === ConditionTypeEnum.IS_NOT_NULL
? `${fieldLabel ?? ''}${conditionType ?? ''}`
: key === 'field'
? currentFieldData?.component !== FormElementEnum.SWITCH
? (await setFieldsValue({ value: undefined }), '')
: (await setFieldsValue({ value: 0 }), '否')
: currentFieldData?.component === FormElementEnum.API_SELECT
? ((await getVsList(currentFieldData.vsId)), labelText = fieldVsList?.find((item: Recordable) => item.itemValue === form.value)?.itemText)
: currentFieldData?.component === FormElementEnum.SWITCH
? (labelText = form.value === 1 ? '是' : '否')
: (labelText = form.value, '');
```
这样可以减少代码量,使代码更加简洁。
conditionName = `${fieldLabel}${form.op ? form.op :''}${form.value ? form.value : ''}`简化代码
可以将上述代码简化为:
```
const conditionName = `${fieldLabel}${form.op ?? ''}${form.value ?? ''}`;
```
使用了空值合并运算符(`??`),如果 `form.op` 或 `form.value` 为 `null` 或 `undefined`,则会使用空字符串代替。此外,使用了模板字符串简化字符串拼接。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![tar](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)