kpytom 2023-07-16 13:03 采纳率: 50%
浏览 48
已结题

关于#TS#的问题,如何解决?

怎么解决这两道题啊,刚开始学习不太懂


export interface Space {
  id: string;
  name: string;
  parentId: string;
  subSpaces: Space[];
  selected?: boolean; // 标记路径为 selected
}

// 例如
const spaceTreeList: Space[] = [
  {
    id: "1",
    name: "Space 1",
    parentId: "",
    subSpaces: [
      {
        id: "2",
        name: "Space 1.1",
        parentId: "1",
        selected: true,
        subSpaces: [
          {
            id: "3",
            name: "Space 1.1.1",
            parentId: "2",
            selected: true,
            subSpaces: [
              {
                id: "6",
                name: "Space 1.1.3.2.1",
                parentId: "1",
                selected: true,
                subSpaces: [
                  {
                    id: "8",
                    name: "Space 1.1.1",
                    parentId: "2",
                    selected: true,
                    subSpaces: [],
                  },
                ],
              },
              {
                id: "7",
                name: "Space 1.1.3.4.1",
                parentId: "1",
                selected: false,
                subSpaces: [
                  {
                    id: "9",
                    name: "Space 1.1.1",
                    parentId: "2",
                    selected: false,
                    subSpaces: [],
                  },
                ],
              },
            ],
          },
        ],
      },
    ],
  },
  {
    id: "4",
    name: "Space 2",
    parentId: "",
    subSpaces: [],
    selected: false,
  },
];

// 1. 完成下面方法,要求传入参数为任意的Space数组和一个id,返回仅包含从一级Space到目标Space的路径的数组
// 比如:以上面的 spaceTreeList 为例,当id为 ‘7’时,应返回 [{id: "1",...},{id: "2",...},{id: "3",...},{id: "7",...}]
export function getPaths(tree: Space[], id: string): Space[] {
  return [];
}

// 2. 写一个方法,要求传入参数为任意的Space数组和一个id,要求把在 从一级Space到目标Space的路径 上的所有Space的selected都设为true,其他空间selected都设为false
// 比如:以上面的 spaceTreeList 为例,当id为 ‘7’时,那么id为 1,2,3,7的Space的selected应该为true,其他Space的Selected都应为false

  • 写回答

2条回答 默认 最新

  • 田猿笔记 2023-07-16 13:44
    关注

    以下是getPathssetSelected函数的实现:

    export function getPaths(tree: Space[], id: string): Space[] {
      const paths: Space[] = [];
      
      const findPath = (spaces: Space[], id: string) => {
        for (const space of spaces) {
          if (space.id === id) {
            paths.push(space);
            return true;
          }
          
          if (space.subSpaces && space.subSpaces.length > 0) {
            if (findPath(space.subSpaces, id)) {
              paths.push(space);
              return true;
            }
          }
        }
        
        return false;
      }
      
      findPath(tree, id);
      
      return paths.reverse();
    }
    
    export function setSelected(tree: Space[], id: string): void {
      const paths = getPaths(tree, id);
      
      const traverse = (spaces: Space[]) => {
        for (const space of spaces) {
          if (paths.some(path => path.id === space.id)) {
            space.selected = true;
          } else {
            space.selected = false;
          }
          
          if (space.subSpaces && space.subSpaces.length > 0) {
            traverse(space.subSpaces);
          }
        }
      }
      
      traverse(tree);
    }
    
    
    
    
    

    你可以使用getPaths函数获取到特定空间的路径,并使用setSelected函数根据路径设置空间的selected属性。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月24日
  • 已采纳回答 7月16日
  • 创建了问题 7月16日

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上