xxm-test-js
Version:
xxm-js通用js工具(utils)库
75 lines • 2.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertToTree = convertToTree;
/**
* 将扁平数据结构转换为树形结构。
*
* @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));
* ```
*/
function convertToTree(flatData, options) {
const { idKey, parentIdKey, rootId } = options;
const map = new Map();
const tree = [];
// 检查是否有重复ID
flatData.forEach((item) => {
if (map.has(item[idKey])) {
throw new Error(`Duplicate ID found: ${item[idKey]}`);
}
const node = Object.assign(Object.assign({}, item), { children: [] });
map.set(item[idKey], node);
});
// 建立父子关系
flatData.forEach((item) => {
var _a;
const node = map.get(item[idKey]);
if (item[parentIdKey] === rootId) {
tree.push(node); // 根节点直接添加到树中
}
else {
const parent = map.get(item[parentIdKey]);
if (parent) {
(_a = parent.children) === null || _a === void 0 ? void 0 : _a.push(node); // 添加到父节点的子节点列表
}
else {
console.warn(`Parent not found for item with ID: ${item[idKey]}`);
}
}
});
return tree;
}
//# sourceMappingURL=convertToTree.js.map