-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.js
117 lines (107 loc) · 2.75 KB
/
tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var arr = [
{
"Source": "Health Insurance Market",
"Target": "Agent Service Center",
"calls": "122"
},
{
"Source": "Health Insurance Market",
"Target": "Agent Service Center Services",
"name": "Test2"
},
{
"Source": "Agent Service Center",
"Target": "ASC Blue Diamond 5010",
"name": "Test2"
},
{
"Source": "Agent Service Center",
"Target": "ASC Blue Diamond (5070)",
"name": "Test2"
},
{
"Source": "Agent Service Center Services",
"Target": "ASC Blue Diamond1",
"name": "Test2"
},
{
"Source": "Agent Service Center Services",
"Target": "ASC Blue Diamond2",
"name": "Test2"
},
{
"Source": "Health Insurance Market",
"Target": "Agent Service Center Services1",
"name": "Test2"
},
{
"Source": "ASC Blue Diamond2",
"Target": "ASC Blue Diamond223",
"name": "Test2"
},
{
"Source": "ASC Blue Diamond223",
"Target": "ASC Blue Diamond223677",
"name": "Test2"
},
{
"Source": "ASC Blue Diamond223677",
"Target": "ASC Blue Diamond223677788",
"name": "Test2"
},
{
"Source": "ASC Blue Diamond223677788",
"Target": "ASC Blue Diamond223677788768768",
"name": "Test2"
},
{
"Source": "Health Insurance Market2",
"Target": "Agent Service Center ty",
"calls": "122"
},
];
function convert(dataArray) {
var treeObj = [];
var dataLength = dataArray.length;
var dataObject = null;
var obj = null;
for (var index = 0; index < dataLength; index++) {
dataObject = dataArray[index];
obj = placeIntoTree(treeObj, dataObject.Source);
if (obj.children) {
obj.children.push({
name: dataObject.Target,
children: []
});
} else {
obj.push({
name: dataObject.Source,
children: [{
name: dataObject.Target,
children: []
}]
});
}
}
return treeObj;
}
function placeIntoTree(treeObj, nodeName) {
var obj = null;
debugger;
for (var index = 0; index < treeObj.length; index++) {
obj = treeObj[index];
if (obj.name == nodeName) {
return obj;
}
if (obj.children) {
for (var index1 = 0; index1 < obj.children.length; index1++) {
let obj1 = placeIntoTree(obj.children, nodeName);
if (!Array.isArray(obj1)) {
return obj1;
}
}
}
}
return treeObj;
}
var json = convert(arr);