2017-06-22 8 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

関連する問題