2011-12-13 145 views
2

我的菜单将“selected”数组显示为选项。然后,当选择一个项目时,它会将其分支作为新选项呈现。遍历数组树?

为了跟踪遍历,我创建了一个名为select的数组。因此,如果有人捡到了第三选项,然后在第一个选项,然后在第6选项,select = [3,1,6]

这是很容易只是推动指数进入阵列,我的问题是我如何使用这个数组创建树的参考?

如果select是[3,1,6]我想要创建一个函数,该函数可以引用tree[3][1][6],这也允许我通过截去数组的最后一个值来向后遍历。

(在CoffeeScript中)

tree: 
    name: 'name1' 
    branches:[ 
     name: 'name2' 
     branches: [ 
      name: 'name3' 
      branches: [ 
       name: 'name4' 
       branches:[] 
      , 
       name: 'name5' 
       branches:[] 
      , 
       name: 'name6' 
       branches:[] 
      ] 
     ] 
    ] 

current = tree 

#when clicked 

$('.menu li').on 'click',()-> 
    select.push($(this).index()) 

    for value in select 
     current = current+'['+value+']' 

#this results in a string, not an actual reference to the tree. 

回答

0

如果我理解正确的话,你需要什么,改变了过去的2线,下面应该做的伎俩:

current = tree 
for value in select 
    current = current['branches'][value] 
+0

啊好的,感谢生病去尝试 – fancy