2017-06-22 68 views
0

試圖將嵌套的JSON數據(data1)重新構建爲「正確」格式(data2) 迄今爲止沒有成功。JavaScript ::通過嵌套的JSON對象迭代並創建新的結構

data1基於給定的父目錄(食譜)生成,該目錄查找html文件。

data2是我試圖用data1輸出的內容,因爲文件夾內的任何內容都可以更好地表示爲對象數組,而不僅僅是純粹的嵌套對象。

var data1 = { 
    "cake": { 
     "chocolate": { 
     "black-forest": { 
      "name": "Black Forest", 
      "path": "recipes/cake/chocolate/black-forest.html" 
     }, 
     "new-shortcake": { 
      "milk-chocolate-shortcake": { 
       "name": "Milk chocolate shortcake", 
       "path": "recipes/cake/chocolate/shortcake/milk-chocolate-shortcake.html" 
      }, 
      "dark-chocolate-shortcake": { 
       "name": "Dark chocolate shortcake", 
       "path": "recipes/cake/chocolate/shortcake/dark-chocolate-shortcake.html" 
      } 
     } 
     } 
    }, 
    "pasta": { 
     "spaghetti": { 
     "aglio-olio": { 
      "name": "Spagehetti Aglio Olio", 
      "path": "recipes/pasta/spaghetti/aglio-olio.html" 
     }, 
     "carbonara": { 
      "name": "Carbonara", 
      "path": "recipes/pasta/spaghetti/carbonara.html" 
     } 
     }, 
     "lasagna": { 
     "name": "Lasagna", 
     "path": "recipes/pasta/lasagna.html" 
     } 
    } 
} 



var data2 = [ 
    { 
     "name": "cake", 
     "children": [ 
     { 
      "name": "chocolate", 
      "children": [ 
       { 
        "name": "Black Forest", 
        "path": "recipes/cake/chocolate/black-forest.html" 
       }, 
       { 
        "name": "New Shortcake", 
        "children": [ 
        { 
         "name": "Milk chocolate shortcake", 
         "path": "recipes/cake/chocolate/shortcake/milk-chocolate-shortcake. html" 
        }, 
        { 
         "name": "Dark chocolate shortcake", 
         "path": "recipes/cake/chocolate/shortcake/dark-chocolate-shortcake. html" 
        } 
        ] 
       } 
      ] 
     } 
     ] 
    }, 
    { 
     "name": "pasta", 
     "children": [ 
     { 
      "name": "spaghetti", 
      "children": [ 
       { 
        "name": "Spagehetti Aglio Olio", 
        "path": "recipes/pasta/spaghetti/aglio-olio.html" 
       }, 
       { 
        "name": "Carbonara", 
        "path": "recipes/pasta/spaghetti/carbonara.html" 
       } 
      ] 
     }, 
     { 
      "name": "Lasagna", 
      "path": "recipes/pasta/lasagna.html" 
     } 
     ] 
    } 
] 

https://codepen.io/kyooriouskoala/pen/LLLXmG

任何幫助,非常感謝!

PS:最終目標是建立一個包含新數據結構的菜單。

+0

請將您的代碼直接發佈到您的問題中。 – samanime

+0

@samanime我不能直接發佈整個代碼,因爲它一直說「看起來你的文章主要是代碼;請添加一些更多的細節。」 – kyooriouskoala

+0

你可以給你想要轉換的概要版本嗎? – samanime

回答

0

我希望這個輸出是你的意思。

var final = []; 
function tree(object, temp){ 
for(var key in object){ 

var folder = {}; 
if(object[key] !== null && typeof object[key] == 'object'){ 
    //console.log(key); 

    if(_.has(object[key], "path")){ 
    folder.name = object[key].name; 
    folder.path = object[key].path; 
    folder.children = []; 
    } else{ 
    folder.name = key; 
    folder.children = object[key]; 
    } 

    final.push(folder); 
    tree(object[key]); 
} 
} 

    return final; 
} 

這會將您的數據作爲需要的值輸出爲關聯對象。

+0

謝謝。這不會按預期輸出結果。 – kyooriouskoala