有如下形式的二維數組:array = [ ["北京市","海淀區"], ["北京市","東城區"], ["遼寧省","沈陽市","和平區"], ["遼寧省","沈陽市","鐵西區"], ["臺灣省"]]需要將上面的數組轉換成一個指定格式的對象object:targetObject = { text:"", children: [ { text: '北京市', children: [ { text: '海淀區', children: [], },{ text: '東城區', children: [] } ] },{ text: '遼寧省', children: [ { text: '沈陽市', children: [ { text: '和平區', children: [] },{ text: '鐵西區', children: [] } ] } ] },{ text: '臺灣省', children: [] } ]}可能看起來有點復雜,不過思路應該還是挺清晰的,應該是一個遞歸的過程,但是本人算法是在不是很好,求大神給一個解決方案
算法小問題:指定類型數組轉換成對象的問題。
素胚勾勒不出你
2019-02-13 17:14:45
