const addType = checkedList.find((item) => !current.includes(item)); console.log('添加类型', addType); const row = { id: current.length, data_type: addType, group_id: undefined, sort_type: 'DESC', } // 小计 if (addType === 'subtotal') { dynamicStatisticForm.statistics.push(row) } else if (addType === 'total') { // 总计 dynamicStatisticForm.statistics.unshift(row) } else { // 合计 if (current.length) { if (current.length === 1) { // 总计或者小计 if (current[0] === 'total') { dynamicStatisticForm.statistics.push(row) } else if (current[0] === 'subtotal'){ dynamicStatisticForm.statistics.unshift(row) } } else { // 总计和小计 dynamicStatisticForm.statistics.splice(1, 0, row) } } else { dynamicStatisticForm.statistics.unshift(row) } }简化代码
时间: 2023-11-28 10:51:40 浏览: 32
可以将代码简化如下:
```
const addType = checkedList.find((item) => !current.includes(item));
console.log('添加类型', addType);
const row = {
id: current.length,
data_type: addType,
group_id: undefined,
sort_type: 'DESC',
};
if (addType === 'subtotal') {
dynamicStatisticForm.statistics.push(row);
} else if (addType === 'total') {
dynamicStatisticForm.statistics.unshift(row);
} else {
if (current.length && current.length === 1) {
if (current[0] === 'total') {
dynamicStatisticForm.statistics.push(row);
} else if (current[0] === 'subtotal') {
dynamicStatisticForm.statistics.unshift(row);
}
} else {
dynamicStatisticForm.statistics.splice(1, 0, row);
}
}
```
简化后的代码主要是对嵌套的 if-else 语句进行了优化,使得逻辑更加清晰。同时,也将一些变量的定义和赋值合并在了一起,使得代码更加简洁。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)