Skip to content

遍历树结构

  • 需求

    遍历树结构

  • 代码

    interface ITExtends {
      children?: []
      level?
    }
    
    /**
     * 遍历树结构, 调用 treeForeach(tree, node => { console.log(node.title) })
    * @param tree
    * @param func
    */
    export const treeForeach = <T extends ITExtends>(tree: Array<T>, func): void => {
      tree.forEach((data) => {
        data.children && treeForeach(data.children, func) // 遍历子树
        func(data)
      })
    }