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 { dynamicStatisticForm.statistics.unshift(row) } } else { // 总计和小计 dynamicStatisticForm.statistics.splice(1, 0, row) } } else { dynamicStatisticForm.statistics.unshift(row) } }。简化代码
时间: 2023-11-28 14:51:22 浏览: 28
这段代码的作用是根据条件向 `dynamicStatisticForm.statistics` 数组中添加对象 `row`。具体实现逻辑如下:
1. 从 `checkedList` 数组中找到第一个不在 `current` 数组中的元素,赋值给变量 `addType`。
2. 根据 `addType` 的值,确定 `row` 中的 `data_type` 和数组添加的位置。
- 如果 `addType` 的值为 `'subtotal'`,将 `row` 添加到数组末尾。
- 如果 `addType` 的值为 `'total'`,将 `row` 添加到数组开头。
- 如果 `addType` 的值为其他值,需要根据当前数组中元素的情况来决定添加的位置。具体逻辑如下:
- 如果当前数组为空,将 `row` 添加到数组开头。
- 如果当前数组只有一个元素,且该元素的值为 `'total'`,将 `row` 添加到数组末尾;否则将 `row` 添加到数组开头。
- 如果当前数组有多个元素,将 `row` 添加到第二个位置。
3. 将 `row` 添加到 `dynamicStatisticForm.statistics` 数组中。
简化后的代码如下:
```
const addType = checkedList.find(item => !current.includes(item));
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) {
dynamicStatisticForm.statistics.unshift(row);
} else if (current.length === 1 && current[0] === 'total') {
dynamicStatisticForm.statistics.push(row);
} else {
dynamicStatisticForm.statistics.splice(1, 0, row);
}
}
```
相关推荐
![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/20210720083646.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)