SOURCE

const treeDataArray = [
  {
    id: 1,
    name: "Root",
    children: [
      {
        id: 2,
        name: "Child 1",
        children: [
          {
            id: 4,
            name: "Child 1.1",
            children: []
          },
          {
            id: 5,
            name: "Child 1.2",
            children: []
          }
        ]
      },
      {
        id: 3,
        name: "Child 2",
        children: [
          {
            id: 6,
            name: "Child 2.1",
            children: []
          },
          {
            id: 7,
            name: "Child 2.2",
            children: []
          }
        ]
      }
    ]
  }
];
function findParentNode(arr, childId, parent = null) {
  for (const item of arr) {
    if (item.id === childId) {
      return parent;
    }
    if (item.children && item.children.length > 0) {
      const found = findParentNode(item.children, childId, item);
      if (found) {
        return found;
      }
    }
  }
  return null;
}

// 使用示例
const childId = 5;
const parentNode = findParentNode(treeDataArray, childId);

console.log(parentNode); // 输出:{ id: 2, name: 'Child 1', children: [ { id: 4, name: 'Child 1.1', children: [] }, { id: 5, name: 'Child 1.2', children: [] } ] }
console 命令行工具 X clear

                    
>
console