perf: 同步完整版代码
This commit is contained in:
@@ -65,3 +65,55 @@ export function buildHierarchyTree(menuTree, pathList = []) {
|
||||
}
|
||||
return menuTree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 广度优先遍历算法,找当前节点
|
||||
* @param {Array} tree 原始树,数组
|
||||
* @param {Number|String} uniqueId 唯一uniqueId
|
||||
* @return {Object} node
|
||||
*/
|
||||
export function getNodeByUniqueId(menuTree, uniqueId) {
|
||||
if (!Array.isArray(menuTree)) {
|
||||
console.warn("menuTree must be an array");
|
||||
return;
|
||||
}
|
||||
if (!menuTree || menuTree.length === 0) return;
|
||||
const item = menuTree.find(node => node.uniqueId === uniqueId);
|
||||
if (item) return item;
|
||||
const childrenList = menuTree
|
||||
.filter(node => node.children)
|
||||
.map(i => i.children)
|
||||
.flat(1);
|
||||
return getNodeByUniqueId(childrenList, uniqueId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 向当前唯一uniqueId节点追加字段
|
||||
* @param {Array} {menuTree 菜单树}
|
||||
* @param {Number|String} uniqueId 唯一uniqueId
|
||||
* @param {Object} fields 唯一uniqueId
|
||||
* @return {menuTree} 追加字段后的树
|
||||
*/
|
||||
export function appendFieldByUniqueId(
|
||||
menuTree: Array<any>,
|
||||
uniqueId: Number | String,
|
||||
fields: Object
|
||||
) {
|
||||
if (!Array.isArray(menuTree)) {
|
||||
console.warn("menuTree must be an array");
|
||||
return;
|
||||
}
|
||||
if (!menuTree || menuTree.length === 0) return {};
|
||||
for (const node of menuTree) {
|
||||
const hasChildren = node.children && node.children.length > 0;
|
||||
if (
|
||||
node.uniqueId === uniqueId &&
|
||||
Object.prototype.toString.call(fields) === "[object Object]"
|
||||
)
|
||||
Object.assign(node, fields);
|
||||
if (hasChildren) {
|
||||
appendFieldByUniqueId(node.children, uniqueId, fields);
|
||||
}
|
||||
}
|
||||
return menuTree;
|
||||
}
|
||||
|
Reference in New Issue
Block a user