🌺前言
获取子树(包括当前子节点)
javascript
/**
* 获取子树以及该节点
* @param {array} nodes 节点数组
* @param {string} target 目标节点id
* @param {object} props {id:,children}的配置
*/
export default function useDeepFindTreeChildren (
nodes,
target,
props = { id: 'id', children: 'children' }
) {
const result = []
function findNode (arr, tag, type = false, props) {
if (!arr || !arr.length) {
return
}
if (type) {
for (let i = 0; i < arr.length; i++) {
result.push(arr[i])
if (arr[i][props.children] && arr[i][props.children].length) {
findNode(arr[i][props.children], tag, true, props)
}
}
} else {
for (let i = 0; i < arr.length; i++) {
if (arr[i][props.id] === tag) {
result.push(arr[i])
if (arr[i][props.children] && arr[i][props.children].length) {
findNode(arr[i][props.children], tag, true, props)
}
} else {
if (arr[i][props.children] && arr[i][props.children].length) {
findNode(arr[i][props.children], tag, false, props)
}
}
}
}
}
findNode(nodes, target, false, props)
return result
}
文章最后更新于 2024-08-20 16:16:03
作者:徐徐版权声明:转载请注明文章出处
留言

~~空空如也