🌺前言
JS获取一颗树的一个节点
javascript
/**
* 查询一个树的子节点
* @param {object} nodes 树
* @param {string} target 目标节点id
* @param {object} props
* @returns
*/
export default function useDeepFindTreeNode (
nodes,
target,
props = { id: 'id', children: 'children' }
) {
let result = null
function findNode (arr, tag, props) {
if (!arr || !arr.length) {
return
}
for (let i = 0; i < arr.length; i++) {
if (arr[i][props.id] === tag) {
result = arr[i]
break
} else if (arr[i][props.children] && arr[i][props.children].length) {
findNode(arr[i][props.children], tag, props)
}
}
}
findNode(nodes, target, props)
return result
}
文章最后更新于 2024-08-20 16:15:07
作者:徐徐版权声明:转载请注明文章出处
留言

~~空空如也