el-cascader已知子节点id获取父级及以上id

// 反显获取门名称
const cascaderCode = (array, id) => {
  let stack = []
  let going = true
  let walker = (array, id) => {
    array.forEach((item) => {
      if (!going) return
      stack.push({ id: item['id'], label: item['deptName'] })
      if (item['id'] == id) {
        going = false
      } else if (item['children']) {
        walker(item['children'], id)
      } else {
        stack.pop()
      }
    })
    if (going) stack.pop()
  }
  walker(array, id)
  return stack
}

返回对象

你可能感兴趣的:(前端,javascript,开发语言)