根据ids keys 找他祖祖父父的层级数据结构
function findAncestors(data, ids, keys = {}) {
const result = [];
for (const node of data) {
node.menuName = node.label;
node.resourceName = node.label;
if (ids.includes(node.id)) {
result.push(node);
keys[node.id] = true; // 将已找到的节点记录在 keys 中
}
if (node.children) {
const subResult = findAncestors(node.children, ids, keys);
if (subResult.length > 0 && !keys[node.id]) {
// 检查子节点是否已经在祖先节点中添加
result.push({
...node,
children: subResult,
});
}
}
}
return result;
}