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