mt-flowbite-react
Version:
Official React components built for Flowbite and Tailwind CSS
44 lines (43 loc) • 1.21 kB
JavaScript
// 递归查找子节点的函数
export function findChildrenById(root, targetId) {
if (!root) {
return null;
}
console.log('findChildrenById: ', root.id, targetId);
// 如果当前节点的ID与目标ID匹配,则返回当前节点
if (root.id === targetId) {
return root;
}
// 递归查找子节点
if (root.children) {
for (const child of root.children) {
const found = findChildrenById(child, targetId);
if (found) {
return found;
}
}
}
// 如果没有找到匹配的子节点,则返回null
return null;
}
export function findNodeById(root, id) {
if (!root) {
return null;
}
console.log('findNodeById: ', root.id, id);
// 如果当前节点的ID与目标ID匹配,则返回当前节点
if (root.id === id) {
return root;
}
// 递归查找子节点
if (root.children) {
for (const child of root.children) {
const found = findNodeById(child, id);
if (found) {
return found;
}
}
}
// 如果没有找到匹配的子节点,则返回null
return null;
}