js 递归给父节点命名赋值

 // 递归给父节点名字赋值
    getDepName(data) {
      data.forEach((v) => {
        if (v.pid == 0) {
          v["parentName"] = "无";
        }
        if (v.children && v.children.length > 0) {
          v.children.forEach((v2) => {
            if (v.id == v2.pid) {
              v2["parentName"] = v.name;
            }
          });
          this.getDepName(v.children);
        }
      });
      return data;
    },

数据:data

[
    {
        "id": 100000022,
        "number": 1,
        "name": "测试公司",
        "type": "维护单位",
        "owner": "",
        "tel": "",
        "peopleNum": 1,
        "enabled": true,
        "description": "",
        "children": [
            {
                "id": 100000023,
                "number": 1,
                "name": "测试部门",
                "type": "维护单位",
                "owner": "",
                "tel": "",
                "peopleNum": 1,
                "enabled": true,
                "description": "",
                "children": [],
                "userList": [],
                "pid": 100000022,
                "orderId": 900000019,
                "firstCategoryId": "",
                "firstCategory": ""
            }
        ],
        "userList": [],
        "pid": 0,
        "orderId": 900000020,
        "firstCategoryId": "",
        "firstCategory": ""
    },
    {
        "id": 100000079,
        "number": 2,
        "name": "一级部门",
        "type": "维护单位",
        "owner": "aa",
        "tel": "18601614512",
        "peopleNum": 1,
        "enabled": true,
        "description": "",
        "children": [
            {
                "id": 100000080,
                "number": 1,
                "name": "二级部门",
                "type": "维护单位",
                "owner": "2222",
                "tel": "18601614512",
                "peopleNum": 1,
                "enabled": true,
                "description": "",
                "children": [],
                "userList": [],
                "pid": 100000079,
                "orderId": 900000080,
                "firstCategoryId": "",
                "firstCategory": ""
            }
        ],
        "userList": [],
        "pid": 0,
        "orderId": 900000079,
        "firstCategoryId": "",
        "firstCategory": ""
    },
    {
        "id": 100000082,
        "number": 3,
        "name": "科技科",
        "type": "维护单位",
        "owner": "aaa",
        "tel": "18611111111",
        "peopleNum": 14,
        "enabled": true,
        "description": "",
        "children": [
            {
                "id": 100000083,
                "number": 1,
                "name": "a科室",
                "type": "维护单位",
                "owner": "aaa",
                "tel": "18601614512",
                "peopleNum": 5,
                "enabled": true,
                "description": "",
                "children": [],
                "userList": [],
                "pid": 100000082,
                "orderId": 900000083,
                "firstCategoryId": "",
                "firstCategory": ""
            },
            {
                "id": 100000085,
                "number": 2,
                "name": "b科室",
                "type": "维护单位",
                "owner": "112",
                "tel": "18601614512",
                "peopleNum": 10,
                "enabled": true,
                "description": "",
                "children": [],
                "userList": [],
                "pid": 100000082,
                "orderId": 900000085,
                "firstCategoryId": "",
                "firstCategory": ""
            },
            {
                "id": 100000123,
                "number": 3,
                "name": "c部门",
                "type": "",
                "owner": "",
                "tel": "",
                "peopleNum": 0,
                "enabled": true,
                "description": "",
                "children": [],
                "userList": [],
                "pid": 100000082,
                "orderId": 900000123,
                "firstCategoryId": "",
                "firstCategory": ""
            }
        ],
        "userList": [],
        "pid": 0,
        "orderId": 900000082,
        "firstCategoryId": "",
        "firstCategory": ""
    },
    {
        "id": 100000091,
        "number": 4,
        "name": "测试1",
        "type": "维护单位",
        "owner": "测试1",
        "tel": "18798567896",
        "peopleNum": 0,
        "enabled": true,
        "description": "",
        "children": [],
        "userList": [],
        "pid": 0,
        "orderId": 900000091,
        "firstCategoryId": "",
        "firstCategory": ""
    },
    {
        "id": 100000106,
        "number": 5,
        "name": "测试部门1",
        "type": "维护单位",
        "owner": "测试部门1",
        "tel": "13525354555",
        "peopleNum": 0,
        "enabled": true,
        "description": "",
        "children": [
            {
                "id": 100000120,
                "number": 1,
                "name": "测试部门111",
                "type": "",
                "owner": "",
                "tel": "",
                "peopleNum": 0,
                "enabled": true,
                "description": "",
                "children": [
                    {
                        "id": 100000122,
                        "number": "",
                        "name": "测试部门11111111",
                        "type": "",
                        "owner": "",
                        "tel": "",
                        "peopleNum": 0,
                        "enabled": true,
                        "description": "",
                        "children": [],
                        "userList": [],
                        "pid": 100000120,
                        "orderId": 900000122,
                        "firstCategoryId": "",
                        "firstCategory": ""
                    }
                ],
                "userList": [],
                "pid": 100000106,
                "orderId": 900000120,
                "firstCategoryId": "",
                "firstCategory": ""
            }
        ],
        "userList": [],
        "pid": 0,
        "orderId": 900000106,
        "firstCategoryId": "",
        "firstCategory": ""
    },
    {
        "id": 100000107,
        "number": 6,
        "name": "测试部门2",
        "type": "维护单位",
        "owner": "测试部门2",
        "tel": "13525354555",
        "peopleNum": 0,
        "enabled": true,
        "description": "",
        "children": [],
        "userList": [],
        "pid": 0,
        "orderId": 900000107,
        "firstCategoryId": "",
        "firstCategory": ""
    },
    {
        "id": 100000108,
        "number": 7,
        "name": "测试部门3",
        "type": "维护单位",
        "owner": "测试部门3",
        "tel": "13525354555",
        "peopleNum": 0,
        "enabled": true,
        "description": "",
        "children": [],
        "userList": [],
        "pid": 0,
        "orderId": 900000108,
        "firstCategoryId": "",
        "firstCategory": ""
    },
    {
        "id": 100000109,
        "number": 8,
        "name": "测试部门4",
        "type": "维护单位",
        "owner": "测试部门4",
        "tel": "13525354555",
        "peopleNum": 0,
        "enabled": true,
        "description": "",
        "children": [],
        "userList": [],
        "pid": 0,
        "orderId": 900000109,
        "firstCategoryId": "",
        "firstCategory": ""
    },
    {
        "id": 100000110,
        "number": 9,
        "name": "测试部门5",
        "type": "维护单位",
        "owner": "测试部门5",
        "tel": "13525354555",
        "peopleNum": 0,
        "enabled": true,
        "description": "",
        "children": [],
        "userList": [],
        "pid": 0,
        "orderId": 900000110,
        "firstCategoryId": "",
        "firstCategory": ""
    }
]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值