// 根据子id找父id,返回值不包含查找的id
function findParentsId (treeData, id) {
if(treeData.length == 0) return
for(let i = 0; i < treeData.length; i++) {
if(treeData[i].id == id) {
return []
}else{
if(treeData[i].children) {
let res = findParentsId(treeData[i].children, id)
if(res !== undefined) {
//这里也可以返回树节点的name
return res.concat(treeData[i].id).reverse()
}
}
}
}
}
// 组织结构子id,找父id
export const orgChildIdFindParent = (childId, orgList = []) => {
const result = findParentsId(orgList, childId).concat(childId)
return result || []
}
// 使用
console.log(orgChildIdFindParent(1, list))
网友评论