分类-递归

发布时间:2024年01月10日
 "data": [
        {
            "id": 1,
            "pId": 0,
            "name": "一级1"
        },
        {
            "id": 2,
            "pId": 0,
            "name": "一级2"
        },
        {
            "id": 3,
            "pId": 1,
            "name": "二级1"
        },
        {
            "id": 4,
            "pId": 2,
            "name": "二级2"
        },
        {
            "id": 5,
            "pId": 3,
            "name": "三级1"
        },
        {
            "id": 6,
            "pId": 3,
            "name": "三级2"
        },
        {
            "id": 7,
            "pId": 3,
            "name": "三级3"
        },
        {
            "id": 8,
            "pId": 4,
            "name": "三级4"
        }
    ]
getTree(pId, arr) {
                let children = []
                for(let i = 0; i < arr.length; i++){
                    if(arr[i].pId == pId) {
                        arr[i].children = this.getTree(arr[i].id,arr)
                        children.push(arr[i])
                    }
                }
                return children
			},

文章来源:https://blog.csdn.net/Jk200165/article/details/135506839
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。