Loading

工具函数-js遍历树形结构(Tree),根据id找到对应的name(递归)

JS-遍历树形结构(Tree),根据id找到对应的name(递归)

getTreeName(list, id){
  let _this = this
  for (let i = 0; i < list.length; i++) {
    let a = list[i]
    if (a.id === id) {
      return a.name
    } else {
      if (a.children && a.children.length > 0) {
        let res = _this.getTreeName(a.children, id)
        if (res) {
          return res
        }
      }
    }
  }
}
posted @ 2021-12-06 15:09  顾诚的城  阅读(936)  评论(0编辑  收藏  举报