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.
啊好的,感謝生病去嘗試 – fancy