for (let item of list) { let addchildren=[] if(item.orgDepartment.length>0){ let org_id = item.orgDepartment.map(e=>e.org_id) addchildren = this.orgList.filter(e=>org_id.includes(e.value)) for(let ite of item.orgDepartment){ let adds={ children:[], text:ite.title, value:ite.id, opened: false, selected: false, disabled: false, loading: false, } addchildren.find(e =>e.value === ite.org_id)?.children.push(adds); } console.log(addchildren) } let add={ children:addchildren, text:item.title, value:item.id, opened: false, selected: false, disabled: false, loading: false, } data.find(e =>e.value === item.pid)?.children.push(add); }为什么addchildren叠加了,帮我修改一下代码
时间: 2024-03-25 21:35:22 浏览: 30
为了避免 `addchildren` 叠加的问题,你可以在外层循环的每次迭代中,手动将 `addchildren` 数组重置为空数组。修改后的代码如下:
```javascript
for (let item of list) {
let addchildren = [];
if (item.orgDepartment.length > 0) {
let org_id = item.orgDepartment.map(e => e.org_id);
addchildren = this.orgList.filter(e => org_id.includes(e.value));
for (let ite of item.orgDepartment) {
let adds = {
children: [],
text: ite.title,
value: ite.id,
opened: false,
selected: false,
disabled: false,
loading: false,
};
addchildren.find(e => e.value === ite.org_id)?.children.push(adds);
}
console.log(addchildren);
}
let add = {
children: addchildren,
text: item.title,
value: item.id,
opened: false,
selected: false,
disabled: false,
loading: false,
};
data.find(e => e.value === item.pid)?.children.push(add);
addchildren = []; // 重置 addchildren 数组为空数组
}
```
这样修改后,每次迭代都会使用一个全新的空数组来存储 `addchildren`,从而避免了叠加的问题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)