其他分享
首页 > 其他分享> > 递归查找树中的某个节点

递归查找树中的某个节点

作者:互联网

nodes是树结构数据,id是要查找的树节点的id

const node = this.getNode(nodes, this.id);
console.log(node);


getNode(treeNodes: Array<any>, key) {
    let node;
    for (const treeNode of treeNodes) {
      if (treeNode.Id === key) {
        node = treeNode;
        break;
      }
      if (!Util.IsNullOrEmpty(treeNode.Children)) {
        node = this.getNode(treeNode.Children, key);
        if (node) {
          break;
        }
      }
    }
    return node;
  }

 

标签:node,treeNode,递归,查找,key,const,树中,id,getNode
来源: https://www.cnblogs.com/tingying/p/15850527.html