2022-12-27 树查找节点

93 阅读1分钟

function findNode(tree, func) {  
  for (const node of tree) {  
    if (func(node)) return node  
    if (node.children) {  
      const res = findNode(node.children, func)  
      if (res) return res  
    }  
  }  
  return null  
}  

const data = findNode(tree, (node) => {  //查找符合条件的节点
  return node.name === '节点1-2'  
})  
console.log('找到了:', data)