@gvray/eskit
Version:
A rich and colorful toolkit about typescript and javascript.
74 lines • 2.41 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Converts a flat list of items to a hierarchical tree structure.
* 将扁平的项目列表转换为分层树结构。
*
* @typeParam T - The type of the items in the list / 列表中项目的类型
* @param list - The flat list of items to convert / 要转换的扁平项目列表
* @param pid - The parent ID to start from / 开始的父ID
* @returns The hierarchical tree structure / 分层树结构
*
* @example
* ```typescript
* interface MenuItem {
* id: number
* pid: number | null
* name: string
* children?: MenuItem[]
* }
*
* const flatList: MenuItem[] = [
* { id: 1, pid: null, name: 'Home' },
* { id: 2, pid: null, name: 'Products' },
* { id: 3, pid: 2, name: 'Electronics' },
* { id: 4, pid: 2, name: 'Clothing' },
* { id: 5, pid: 3, name: 'Phones' },
* { id: 6, pid: 3, name: 'Laptops' }
* ]
*
* const tree = listToTree(flatList)
* console.log(tree)
* // [
* // { id: 1, pid: null, name: 'Home', children: [] },
* // {
* // id: 2, pid: null, name: 'Products',
* // children: [
* // {
* // id: 3, pid: 2, name: 'Electronics',
* // children: [
* // { id: 5, pid: 3, name: 'Phones', children: [] },
* // { id: 6, pid: 3, name: 'Laptops', children: [] }
* // ]
* // },
* // { id: 4, pid: 2, name: 'Clothing', children: [] }
* // ]
* // }
* // ]
*
* // Get subtree starting from specific parent
* const electronicsSubtree = listToTree(flatList, 3)
* console.log(electronicsSubtree) // [{ id: 5, ... }, { id: 6, ... }]
* ```
*
* @since 1.0.0
*/
var listToTree = function (list, pid) {
if (pid === void 0) { pid = null; }
return list
.filter(function (item) { return item.pid === pid; })
.map(function (item) { return (__assign(__assign({}, item), { children: listToTree(list, item.id) })); });
};
exports.default = listToTree;
//# sourceMappingURL=listToTree.js.map