需要封装一个递归函数(已去除undefined): function findPathById(tree, targetId) { let path = []; function traverse(node, currentPath) { if (node.id === targetId) { path = [...currentPath, node.id]; return true; // 当找到目标 ID 时返回 true } if (node.children) { for (let child of node.children) { if (traverse(child, [...currentPath, node.id])) { return true; // 如果在子节点中找到目标 ID,则返回 true } } } return false; } for (let node of tree) { if (traverse(node, [])) { break; // 当找到目标 ID 时停止遍历 } } return path; } 传入数组和要查找的id值,如下: findPathById(arr,88)