@supuwoerc/toolkit
    Preparing search index...

    Function getSubtreeByDepth

    • 根据最大深度获取子树 / Get subtree by maximum depth

      该函数递归遍历树形结构,返回一个不超过指定深度的新树。 / This function recursively traverses a tree structure and returns a new tree with depth not exceeding the specified maximum.

      Type Parameters

      • T extends Record<string, any>

        树节点的类型,必须是一个对象 / The type of tree node, must be an object

      • K extends string = "children"

      Parameters

      • tree: T[]

        树形结构的数组 / Array of tree structure

      • maxDepth: number

        最大深度(从0开始计数) / Maximum depth (counting from 0)

      • OptionalchildrenKey: K = ...

        子节点属性名 / Children property name

      • OptionalcurrentDepth: number = 0

        当前深度(内部递归使用) / Current depth (used internally for recursion)

      Returns T[]

      • 处理后的子树数组 / Processed subtree array
      // 示例树结构 / Example tree structure
      const tree = [
      {
      id: 1,
      children: [
      { id: 2, children: [{ id: 3 }] }
      ]
      }
      ];

      // 获取深度为1的子树 / Get subtree with depth 1
      const result = getSubtreeByDepth(tree, 1);
      // 结果: [{ id: 1, children: [{ id: 2 }] }]