用遞迴解析乙個巢狀多層的陣列

2021-10-07 19:31:46 字數 3708 閱讀 5984

let menubean =[[

'images/1.png'

,'頁面1'

,'router1'

,'index1'],

['images/2.png'

,'頁面2'

,null

,'index2',[

'images/2.png'

,'頁面2-1'

,'router2'

,'index3'],

['images/2.png'

,'頁面2-2'

,'router3'

,'index4']]

,['images/3.png'

,'頁面3'

,null

,'index5',[

'images/3.png'

,'頁面3-1'

,null

,'index6',[

'images/3.png'

,'頁面3-1-1'

,'router4'

,'index7']]

,['images/.png'

,'頁面3-2'

,null

,'index8',[

'images/3.png'

,'頁面3-2-1'

,'router5'

,'index9'],

['images/3.png'

,'頁面3-2-2'

,'router6'

,'index10']]

],['images/4.png'

,'頁面4'

,null

,'index11',[

'images/4.png'

,'頁面4-1'

,null

,'index12',[

'images/4.png'

,'頁面4-1-1'

,null

,'index13',[

'images/4.png'

,'頁面4-1-1-1'

,'router7'

,'index14'],

['images/4.png'

,'頁面4-1-1-2'

,'router8'

,'index15']]

],['images/4.png'

,'頁面4-2'

,null

,'index16',[

'images/4.png'

,'頁面4-2-1'

,'router9'

,'index17']]

],];

( 第一項是img,第二項是title,第三項是path,第四項是index,第五項是childs)

let menubean =[[

'images/1.png'

,'頁面1'

,'router1'

,'index1'],

['images/2.png'

,'頁面2'

,null

,'index2',[

'images/2.png'

,'頁面2-1'

,'router2'

,'index3'],

['images/2.png'

,'頁面2-2'

,'router3'

,'index4']]

,['images/3.png'

,'頁面3'

,null

,'index5',[

'images/3.png'

,'頁面3-1'

,null

,'index6',[

'images/3.png'

,'頁面3-1-1'

,'router4'

,'index7']]

,['images/.png'

,'頁面3-2'

,null

,'index8',[

'images/3.png'

,'頁面3-2-1'

,'router5'

,'index9'],

['images/3.png'

,'頁面3-2-2'

,'router6'

,'index10']]

],['images/4.png'

,'頁面4'

,null

,'index11',[

'images/4.png'

,'頁面4-1'

,null

,'index12',[

'images/4.png'

,'頁面4-1-1'

,null

,'index13',[

'images/4.png'

,'頁面4-1-1-1'

,'router7'

,'index14'],

['images/4.png'

,'頁面4-1-1-2'

,'router8'

,'index15']]

],['images/4.png'

,'頁面4-2'

,null

,'index16',[

'images/4.png'

,'頁面4-2-1'

,'router9'

,'index17']]

],];

let menudata =

;var id =0;

for(

var i =

0;i < menubean.length;i++);

obj.id = id++

;

obj.img = menubean[i][0

];obj.title = menubean[i][1

];obj.path = menubean[i][2

];obj.index = menubean[i][3

];menudata.

push

(obj)

; obj.childs =

;if(menubean[i]

.length >4)

else

for(

var j =

4;j < menubean[i]

.length;j++)}

//建立二級選單

function

generatesubmenu

(parent,submenubean)

; obj.id = id++

; obj.img = submenubean[0]

; obj.title = submenubean[1]

; obj.path = submenubean[2]

; obj.index = submenubean[3]

; obj.parent = parent;

obj.childs =

;if(submenubean.length >4)

}else

return obj;

} console.

log(

'****menudata='

,menudata)

用proxy寫乙個遞迴來監聽巢狀物件甚至JSON

之前的這篇文章 其實那個監聽方式有個大bug,當不停的update 監聽物件以後就會導致記憶體洩漏。因為每次在呼叫get的時候都會生成乙個新的proxy,後來才知道是多傻。經過我一天時間的思考,用了乙個遞迴的方法去解決巢狀物件的監聽問題。下面是 傳遞兩個引數,乙個是object,乙個是proxy的h...

js中如何優雅修改乙個多層巢狀list物件的值

如何優雅更改乙個n層巢狀物件屬性的值dp.data.treelist dp.data.treelist.map var obj obj.list.update console.log obj 假設有乙個list類,接收乙個list的核心屬性,其它傳入的屬性也通過 others 自動新增 functi...

用遞迴演算法判斷乙個陣列是否遞增

本題要求使用遞迴演算法,設陣列為array,則遞迴陣列滿足以下條件。1 如果陣列長度為1,則該陣列為遞增,返回true。2 如果陣列長度為n n 2 則先比較最後兩個元素是否遞增,如果最後兩個元素遞增,則再遞迴比較除去最後乙個元素的前 n 1 個元素是否遞增。具體實現如下 include bool ...