UNPKG

xxm-test-js

Version:

xxm-js通用js工具(utils)库

51 lines (50 loc) 1.57 kB
/** * 将扁平数据结构转换为树形结构。 * * @template T 泛型类型,表示节点对象的类型。 * * @param {T[]} flatData - 扁平数据数组。 * @param {TreeOptions<T>} options - 树形结构的配置选项。 * @returns {Array<TreeNode<T>>} 转换后的树形结构数组。 * * @throws {Error} 如果存在重复 ID 或找不到父节点时抛出错误。 * * @example * ```typescript * // 假设我们有以下扁平数据结构: * interface DataItem { * id: number; * name: string; * parentId?: number | null; * } * * const flatData: DataItem[] = [ * { id: 1, name: 'Root', parentId: null }, * { id: 2, name: 'Child 1', parentId: 1 }, * { id: 3, name: 'Child 2', parentId: 1 }, * { id: 4, name: 'Grandchild 1', parentId: 2 }, * { id: 5, name: 'Grandchild 2', parentId: 2 }, * { id: 6, name: 'Another Root', parentId: null } * ]; * * const options: TreeOptions<DataItem> = { * idKey: 'id', * parentIdKey: 'parentId', * rootId: null, * }; * * // 使用 convertToTree 函数将扁平数据转换为树形结构 * const treeData = convertToTree(flatData, options); * console.log(JSON.stringify(treeData, null, 2)); * ``` */ export declare function convertToTree<T extends Record<string, any>>(flatData: T[], options: TreeOptions<T>): Array<TreeNode<T>>; type TreeNode<T extends Record<string, any>> = T & { children?: Array<TreeNode<T>>; }; interface TreeOptions<T extends Record<string, any>> { idKey: keyof T; parentIdKey: keyof T; rootId: T[keyof T]; } export {};