递归方法整合

 1 /*菜单树下拉,禁止选择当前项及其子项,以防止死循环*/
 2 /*item表示递归树,compID表示比对的需要禁止选择的项ID*/
 3 // 实例:编辑情况下,禁止选择当前节点及其子节点
 4 export function diGuiTreeEdit(item, compID) {
 5   const dataSor = item
 6   let treeAry = []
 7   for (let i in dataSor) {
 8     const v = dataSor[i]
 9     let node = {}
10     if (v === null || v === undefined) { }
11     else {
12       if (v.children && v.children.length > 0) {
13         if (v.id == compID) {
14           node.isDisabled = true
15         } else {
16           node.isDisabled = false
17         }
18         node.id = v.id
19         node.label = v.label
20         node.name = v.name
21         node.children = diGuiTreeEdit(v.children, compID)
22         treeAry.push(node)
23       } else {
24         if (v.id == compID) {
25           node.isDisabled = true
26         } else {
27           node.isDisabled = false
28         }
29         node.id = v.id
30         node.label = v.label
31         node.name = v.name
32         treeAry.push(node)
33       }
34     }
35   }
36   return treeAry
37 }
 1 // 递归遍历根据id,找到对应的node节点,得到一个结果。treeAry主要是用于递归push的值
 2 // item表示递归树,id表示已知的值的在对象中的key,idVal表示已知的数值。treeAry用于存放每一步遍历得出的值
 3 // 例如根据id在递归树中获取其名称
 4 export function rspTreeNodeFormID(item,id,idVal, treeAry=[]) {
 5   const dataSor = item
 6   for (let v of dataSor) {
 7     if (v === null || v === undefined) { }
 8     else {
 9       if (v.children && v.children.length > 0) {
10         if (v[id] == idVal) {
11           treeAry.push(v)
12           break
13         } else {
14           rspTreeNodeFormID(v.children, id, idVal,treeAry )
15         }
16       } else {
17         if (v[id] == idVal) {
18           treeAry.push(v)
19           break
20         }
21       }
22     }
23   }
24   return treeAry
25 }

 1 // 递归遍历根据id,找到对应的node节点下的data属性里面的对应的值。treeAry主要是用于递归push的值
 2 // 和方法rspTreeNodeFormID实现的功能类似
 3 export function rspTreeNodeDataFormID(item,firstAttr,secAttr,idVal, treeAry=[]) {
 4   const dataSor = item
 5   for (let v of dataSor) {
 6     if (v === null || v === undefined) { }
 7     else {
 8       if (v.children && v.children.length > 0) {
 9         if (v[firstAttr] && v[firstAttr][secAttr] && v[firstAttr][secAttr] == idVal) {
10           treeAry.push(v)
11           break
12         } else {
13           rspTreeNodeDataFormID(v.children, firstAttr,secAttr, idVal,treeAry )
14         }
15       } else {
16         if (v[firstAttr] && v[firstAttr][secAttr] && v[firstAttr][secAttr] == idVal) {
17           treeAry.push(v)
18           break
19         }
20       }
21     }
22   }
23   return treeAry
24 }
原文地址:https://www.cnblogs.com/luoxuemei/p/12171780.html