检索关键词节点及其所有父节点
any86 opened this issue · 0 comments
any86 commented
/**
* 检索关键词节点及其所有父节点
* @param {String} keywords
* @param {Array} data
* @returns 关键词节点及其所有父节点
*/
function findRelativeNode(keywords, data) {
// 根据id做映射
const map = {};
for (const item of data) {
map[item.groupCode] = item;
}
// 记录已经存储的id
const _idMap = {};
// 存储结果
const rows = [];
for (let key in map) {
const item = map[key];
if (item.groupName.includes(keywords)) {
// 存储子节点
rows.push(item);
// 记录查询过的父id
_idMap[item.groupCode] = 1;
// 找父节点
let _parent = map[item.parentCode];
while (void 0 !== _parent && 1 != _idMap[_parent.groupCode]) {
rows.push(_parent);
_idMap[_parent.groupCode] = 1;
_parent = map[_parent.parentCode];
}
}
}
return rows;
}