// 找出指定id对应数据所有的父级元素
export function findParentElements(tree, targetId,childKey, parents = []) {
if (!childKey) childKey = "children";
for (let i = 0; i < tree.length; i++) {
const node = tree[i];
if (node.id === targetId) {
return parents;
}
if (node[childKey]) {
const found = findParentElements(node[childKey], targetId,childKey, [...parents, node]);
if (found) {
return found;
}
}
}
return null;
}
JS找出指定id对应数据所有的父级元素
于 2024-03-14 16:33:43 首次发布