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>