SOURCE

let arr = [
    {
        id: 1,
        type: 'dom',
        children: [
            {
                id: 2,
                type: 'html',
                children: [
                    {
                        id: 5,
                        type: 'node'
                    }
                ]
            }
        ]
    },
    {
        id: 3,
        type: 'css',
        children: [
            {
                id: 4,
                type: 'javascript'
            }
        ]
    }
]

function returnPath (arr,target){
    let str =''
    function digui(array,path=''){
        for(let i = 0;i<array.length;i++){
            if(array[i].id==target){
                str=path+'/'+array[i].type
                return
            }
            if(array[i].children?.length && array[i].children.includes(item=>item.id===target)){
                str+=array[i].type+'/'
            }
            if(array[i]?.children?.length){
                fpath=path+'/'+array[i].type
                digui(array[i].children,fpath)
            }
        }
    }
    digui(arr)
    return str
}
console.log(returnPath(arr,5))
console 命令行工具 X clear

                    
>
console