js,递归获取所有父级Id

阅读 60

2022-03-10

// 获取所有父级ID
  function  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].path) {
                  return tmpPath
              }
              if (arr[i].children !== null) {
                  const findResult = getActiveMenuId(path, arr[i].children, tmpPath)
                  if (findResult) {
                      return findResult
                  }
              }
          }
        }
    }
// 调用
getActiveMenuId(path,arr)

精彩评论(0)

0 0 举报