// 根据当前id获取从根节点到该节点的路径
const getPathById = (id, data) => {
const get = (children, target, record = []) =>
children.reduce((result, cur) => {
if (cur.id === target) {
return [...record, cur]
}
if (cur.children && cur.children.length) {
return [...result, ...get(cur.children, target, [...record, cur])]
}
return result
}, [])
return get(data, id)
}
网友评论