js 樹形結構 遞迴演算法
阿新 • • 發佈:2019-02-14
function GetData(id, arry) {
var childArry = GetParentArry(id, arry);
if (childArry.length > 0) {
for (var i in childArry) {
if (childArry[i].super_unid == undefined) {
tree.push({
unid: childArry[i].unid,
label: childArry[i].name,
unit_unid:childArry[i].unit_unid,
backgroudImage: childArry[i].graph_url||require('../assets/img/bg.png'),
containerHight: 400, //底圖高度
containerWidth: 300, //底圖寬度
children: []
});
pos[childArry[i].unid] = [tree.length - 1];
} else {
var posArr = pos[childArry[i].super_unid];
if (posArr != undefined) {
var obj = tree[posArr[0]];
for (var j = 1; j < posArr.length; j++) {
obj = obj.children[posArr[j]];
}
obj.children.push({
unid: childArry[i].unid,
label: childArry[i].name,
unit_unid:childArry[i].unit_unid,
backgroudImage:childArry[i].graph_url||require('../assets/img/svg.svg'),
containerHight: 400, //底圖高度
containerWidth: 300, //底圖寬度
children: []
});
pos[childArry[i].unid] = posArr.concat([obj.children.length - 1]);
}
}
GetData(childArry[i].unid, arry);
}
}
}
function GetParentArry(id, arry) {
var newArry = new Array();
for (var i in arry) {
if (arry[i].super_unid == id)
newArry.push(arry[i]);
}
return newArry;
}