const getTreeIds = (tree: any[], nodeId: any, config: {}) => {
//tree目标树,nodeId节点id
const { children = 'cityList', id = 'adCode' } = config || {}
const toFlatArray = (tree: any[], parentId: never[]) => {
return tree.reduce((t: any, _: Record<string, any>) => {
const child = _[children]
return [
...t,
parentId ? { ..._, parentId } : _,
...(child && child.length ? toFlatArray(child, _[id]) : [])]
}, [])
}
const getIds = (flatArray: any[]) => {
let ids = [nodeId]
let child = flatArray.find((_: Record<string, any>) => _[id] === nodeId)
while (child && child.parentId) {
ids = [child.parentId, ...ids]
// eslint-disable-next-line @typescript-eslint/no-loop-func
child = flatArray.find((_: Record<string, any>) => _[id] === child.parentId)
}
return ids
}
return getIds(toFlatArray(tree, []))
}