查找子节点路径

// 查找子节点路径
export function findPath(nodes, targetId, path = []) {
  for (const node of nodes) {
    const currentPath = [...path, node];
    if (node.id === targetId) return currentPath;
    if (node.children && node.children.length) {
      const childPath = findPath(node.children, targetId, currentPath);
      if (childPath) return childPath;
    }
  }
  return null;
}

你可能感兴趣的:(前端,javascript)