buildTree

 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
buildTree = (data )->
# main = x for x in data when not x.parent?
main = data.filter (x) -> not x.parent?
_data = {}
for obj in data when obj.id?
_data[obj.id] = obj
console.log main, _data
chieldRec = (chields) ->
if chields.length == 0
return
res = []
for x in chields
tmp = _data[x]
console.log x, tmp
if tmp.chields.length > 0
rrr = chieldRec tmp.chields
tmp.chields = null
if rrr
tmp.chields = rrr
res.push tmp
res
_tmp = []
for comment in main
t = chieldRec comment.chields
comment.chields = null
if t
comment.chields = t
_tmp.push comment
_tmp