ts/src/utils/tree.js

39 lines
854 B
JavaScript
Raw Normal View History

2024-12-09 06:44:52 +00:00
export const useTree = () => {
return {
filterTreeNodeByField,
2025-02-14 08:33:28 +00:00
getAllKeys,
2024-12-09 06:44:52 +00:00
}
}
function filterTreeNodeByField({ treeData, params, paramName, icon = '' }) {
return treeData.reduce((acc, node) => {
if (params.includes(node[paramName]) && !node.children) {
acc.push({ ...node, icon })
}
if (node.children) {
acc = acc.concat(
filterTreeNodeByField({
treeData: node.children,
params,
paramName,
icon: node.icon,
})
)
}
return acc
}, [])
}
2025-02-14 08:33:28 +00:00
function getAllKeys(treeData, key) {
const data = 'value' in treeData ? treeData.value : treeData
return data.reduce((acc, node) => {
// console.log(node, '---')
acc.push(node[key])
if (node.children) {
acc = acc.concat(getAllKeys(node.children, key))
}
return acc
}, [])
}