elementui table-tree 数据格式转化 如果main_if id相同,把sub_if_number 添加到 子节点下

34 篇文章 0 订阅
9 篇文章 0 订阅

json格式如下:

{
    "devices": [
        {
            "id": 1,
            "name": "device1",
            "interfaces": [
                {
                    "main_if_id": 99,
                    "main_if": "TenGigabit0/0/0/9",
                    "main_type": "phy_int",
                    "sub_if_id": 11,
                    "sub_if_number": 111,
                    "sub_type": "sub_int"
                },
                {
                    "main_if_id": 99,
                    "main_if": "TenGigabit0/0/0/9",
                    "main_type": "phy_int",
                    "sub_if_id": 12,
                    "sub_if_number": 222,
                    "sub_type": "sub_int"
                }
            ]
        },
        {
            "id": 2,
            "name": "device2",
            "interfaces": [
                {
                    "main_if": "Bundle-Ether1",
                    "main_if_id": 21,
                    "sub_if_number": 211
                },
                {
                    "main_if_id": 22,
                    "main_if": "TenGigabit0/0/0/9",
                    "sub_if_number": 212
                }
            ]
        }
    ]
}

转化成===========>


===============================>
{
    "devices": [
        {
            "id": 1,
            "name": "device1",
            "interfaces": [
                {
                    "main_if_id": 99,
                    "main_if": "TenGigabit0/0/0/9",
                    "main_type": "phy_int",
                    "children": [
                        {
                            "sub_if_id": 11,
                            "sub_if_number": 111,
                            "sub_type": "sub_int"
                        },
                        {
                            "sub_if_id": 22,
                            "sub_if_number": 222,
                            "sub_type": "sub_int"
                        }
                    ]
                },
            ]
        },
        {
            "id": 2,
            "name": "device2",
            "interfaces": [
                {
                    "main_if": "Bundle-Ether1",
                    "main_if_id": 21,
                    "sub_if_number": 211
                },
                {
                    "main_if_id": 22,
                    "main_if": "TenGigabit0/0/0/9",
                    "sub_if_number": 212
                }
            ]
        }
    ]
}

方法如下:

<script>
    var a = {
        "devices": [
            {
                "id": 1,
                "name": "device1",
                "interfaces": [
                    {
                        "main_if_id": 99,
                        "main_if": "TenGigabit0/0/0/9",
                        "main_type": "phy_int",
                        "sub_if_id": 11,
                        "sub_if_number": 111,
                        "sub_type": "sub_int"
                    },
                    {
                        "main_if_id": 99,
                        "main_if": "TenGigabit0/0/0/9",
                        "main_type": "phy_int",
                        "sub_if_id": 12,
                        "sub_if_number": 222,
                        "sub_type": "sub_int"
                    },
                    {
                        "main_if_id": 12,
                        "main_if": "Bundle-Ether0"
                    }
                ]
            },
            {
                "id": 2,
                "name": "device1",
                "interfaces": [
                    {
                        "main_if": "Bundle-Ether1",
                        "main_if_id": 21,
                        "sub_if_number": 211
                    },
                    {
                        "main_if_id": 22,
                        "main_if": "TenGigabit0/0/0/9",
                        "sub_if_number": 212
                    }
                ]
            }
        ]
    }
    a.devices.forEach((aItem, index) => {
        let arr = [];
        aItem["children"] = aItem.interfaces
        aItem["expandId"] = Math.random(1, 100) + '';
        aItem.children.forEach(itemChild => {
            if (arr.indexOf(itemChild.main_if) < 0) {
                arr.push(itemChild.main_if);
                itemChild["name"] = itemChild.main_if
                itemChild["expandId"] = Math.random(1, 100) + '';
                let obj = {};
                obj.sub_if_id = itemChild.sub_if_id;
                obj.sub_if_number = itemChild.sub_if_number;
                obj["expandId"] = Math.random(1, 100) + '';
                obj["name"] = itemChild.sub_if_number;
                obj.sub_type = itemChild.sub_type;
                if (!obj.sub_if_number) {
                    itemChild.children = []
                } else {

                    itemChild.children = [obj];
                }
                delete itemChild.sub_type;
                delete itemChild.sub_if_id;
                delete itemChild.sub_if_number;
            }
        })

        arr.forEach(arrItem => {
            let childrenIndex;
            aItem.children.forEach((itemChild, indexs) => {
                if (arrItem === itemChild.main_if && itemChild.children) childrenIndex = indexs;
                if (arrItem === itemChild.main_if && !itemChild.children) {
                    let obj = {};
                    obj.sub_if_id = itemChild.sub_if_id;
                    obj.sub_if_number = itemChild.sub_if_number;
                    obj.sub_type = itemChild.sub_type;
                    obj["name"] = itemChild.sub_if_number;
                    obj["expandId"] = Math.random(1, 100) + '';
                    delete itemChild.sub_type;
                    delete itemChild.sub_if_id;
                    delete itemChild.sub_if_number;
                    aItem.interfaces[childrenIndex].children.push(obj);
                    aItem.interfaces.splice(indexs, 1)
                }
            })

        })
    })

    console.log(a)
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值