-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
66 lines (63 loc) · 1.7 KB
/
index.html
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
<html>
<head>
<script>
const input = [{
pos: 1,
text: 'Andy'
},
{
pos: 1,
text: 'Harry'
},
{
pos: 2,
text: 'David'
},
{
pos: 2,
text: 'Edger'
},
{
pos: 1,
text: 'Lisa'
},
];
const output = [];
const prevIdx = 1;
function createChild(input, output, prevIdx) {
if (input.pos === prevIdx) {
output.push(input);
} else if (input.pos > prevIdx) {
prevIdx += 1;
let children = output[output.length - 1].children;
if (!children) {
children = [];
output[output.length - 1].children = children;
}
createChild(input, children, prevIdx);
} else if (input.pos < prevIdx) {
prevIdx -= 1;
let children = output[output.length - 1].children;
if (!children) {
children = [];
output[output.length - 1].children = children;
}
createChild(input, children, prevIdx);
}
}
function convert () {
for (i=0; i<input.length; i++) {
createChild(input[i], output, prevIdx);
}
console.log(output);
document.getElementById('output').innerText = JSON.stringify(output);
}
</script>
</head>
</html>
<body>
<div id="output"></div>
<script>
convert();
</script>
</body>