【递归汇总】前端各种递归方法记录合集

递归(传入数组,把每一个对象内的id保存到一个数组内返回出去)

getAllId(keys, dataList) {
                if (dataList && dataList.length) {
                    for (let i = 0; i < dataList.length; i++) {
                        keys.push(dataList[i].id)
                        if (dataList[i].children) {
                            keys = this.getAllId(keys, dataList[i].children)
                        }
                    }
                }
                return keys
            },
        }

递归(传入数组和对比值,返回对应id的子级数组)

findChildrenById(arr, id) {
                for (let i = 0; i < arr.length; i++) {
                    if (arr[i].id === id) {
                        return arr[i].children || [];
                    }
                    if (Array.isArray(arr[i].children)) {
                        const result = this.findChildrenById(arr[i].children, id);
                        if (result.length > 0) {
                            return result;
                        }
                    }
                }
                return [];
            },

递归(传入id,数组)用于你传入一个tree结构的数据子级的id或者pid。然后匹配找到所有父节点的id返回给你。如果点击的就是顶级了,没有父级,就会返回false。

findP(id, list = [], result = []) {
                for (let i = 0; i < list.length; i += 1) {
                    console.log('*******', list[i].id, '********')
                    const item = list[i]
                    // 找到目标
                    if (item.id === id) {
                        console.log('找到了')
                        // 加入到结果中
                        result.push(item.id)
                        // 因为可能在第一层就找到了结果,直接返回当前结果
                        if (result.length === 1) return result
                        return true
                    }
                    // 如果存在下级节点,则继续遍历
                    if (item.children) {
                        // 预设本次是需要的节点并加入到最终结果result中
                        result.push(item.id)
                        const find = this.findP(id, item.children, result)
                        // 如果不是false则表示找到了,直接return,结束递归
                        if (find) {
                            return result
                        }
                        // 到这里,意味着本次并不是需要的节点,则在result中移除
                        result.pop()
                    }
                }
                // 如果都走到这儿了,也就是本轮遍历children没找到,将此次标记为false
                return false
            },

递归:传入ID和数组,返回对应id的所有父级,格式:[1,2,3]

    getActiveMenuId(path, arr, paths) {
        if (paths === undefined) {
            paths = []
        }
        if(arr){
          for (let i = 0; i < arr.length; i++) {
              const tmpPath = paths.concat()
              tmpPath.push(arr[i].id)
              if (path === arr[i].id) {
                  return tmpPath
              }
              if (arr[i].children !== null) {
                  const findResult = this.getActiveMenuId(path, arr[i].children, tmpPath)
                  if (findResult) {
                      return findResult
                  }
              }
          }
        }
    },

递归:找到符合条件的数据删除

arr:数组
key:需要传入的判断条件

removeObjects(arr, key) {
                // 数组不存在不执行
                if (!arr) {
                    return
                }
                // 遍历数组  
                for (let i = 0; i < arr.length; i++) {
                    // 检查当前对象是否符合条件  
                    if (arr[i].key == key) {
                        // 删除当前对象  
                        arr.splice(i, 1);
                        // 由于已经删除了一个对象,所以需要调整索引以避免跳过下一个对象  
                        i--;
                    } else {
                        // 如果当前对象不符合条件,则递归检查其子对象  
                        this.removeObjects(arr[i].children, key);
                    }
                }
            },

你可能感兴趣的:(功能实现,前端,vue.js,javascript,递归,el-tree)