2016-04-20 4 views
-3
var data=[{ 

    "name": "cA", 
    "leaf": false, 
    "largeIconId": null, 
    "label": "cA", 
    "hideAllSearchFilters": false, 
    "guidePage": null, 
    "expanded": false, 
    "defaultSearchCategory": false, 
    "childCategories": [{ 
     "name": "cA-A", 
     "leaf": false, 
     "largeIconId": null, 
     "label": "cA-A", 
     "hideAllSearchFilters": false, 
     "guidePage": null, 
     "expanded": false, 
     "defaultSearchCategory": false, 
     "childCategories": [{ 
      "name": "cA-A-A", 
      "leaf": false, 
      "largeIconId": null, 
      "label": "cA-A-A", 
      "hideAllSearchFilters": false, 
      "guidePage": null, 
      "expanded": false, 
      "defaultSearchCategory": false, 
      "childCategories": [{ 
       "name": "cA-A-A-A", 
       "leaf": false, 
       "largeIconId": null, 
       "label": "cA-A-A-A", 
       "hideAllSearchFilters": false, 
       "guidePage": null, 
       "expanded": false, 
       "defaultSearchCategory": false, 
       "childCategories": [{ 
        "name": "cA-A-A-A-A", 
        "leaf": true, 
        "largeIconId": null, 
        "label": "cA-A-A-A-A", 
        "hideAllSearchFilters": false, 
        "guidePage": null, 
        "expanded": false, 
        "defaultSearchCategory": false, 
        "childCategories": [] 
       }] 
      }] 
     }] 
    }, { 
     "name": "cA-B", 
     "leaf": true, 
     "largeIconId": null, 
     "label": "cA-B", 
     "hideAllSearchFilters": false, 
     "guidePage": null, 
     "expanded": false, 
     "defaultSearchCategory": false, 
     "childCategories": [] 
    }, { 
     "name": "cA-C", 
     "leaf": true, 
     "largeIconId": null, 
     "label": "cA-C", 
     "hideAllSearchFilters": false, 
     "guidePage": null, 
     "expanded": false, 
     "defaultSearchCategory": false, 
     "childCategories": [] 
    }] 
}, { 
    "name": "A", 
    "leaf": false, 
    "largeIconId": null, 
    "label": "A", 
    "hideAllSearchFilters": false, 
    "guidePage": null, 
    "expanded": false, 
    "defaultSearchCategory": false, 
    "childCategories": [{ 
     "name": "A-Level1", 
     "leaf": false, 
     "largeIconId": null, 
     "label": "A-Level1", 
     "hideAllSearchFilters": false, 
     "guidePage": null, 
     "expanded": false, 
     "defaultSearchCategory": false, 
     "childCategories": [{ 
      "name": "A-Level2", 
      "leaf": true, 
      "largeIconId": null, 
      "label": "A-Level2", 
      "hideAllSearchFilters": false, 
      "guidePage": null, 
      "expanded": false, 
      "defaultSearchCategory": false, 
      "childCategories": [] 
     }] 
    }] 
}]; 
+1

だから、それを行います。 *特定の*問題に遭遇した場合は、A)で新しい質問を投稿してください。希望の結果を含め、あなたが望むものの明確な説明。 B)入力からその結果を生成しようとするコード。 –

+1

別に:あなたの質問をしていたときに、テキストエリアの右側に大きいオレンジ色**の書式設定**ボックスがあり、そこに有益な情報がありました。また、書式設定支援ツールバー全体がありました。 ** [?] **ボタンを押すと書式設定のヘルプが表示されます。 *また、テキストエリアと投稿の投稿ボタンの間にあるプレビューエリア(ボタンを見つけるために過去のものをスキャンする必要があるように)は、あなたの投稿が投稿時にどのように表示されるかを示します。あなたの投稿をはっきりさせ、時間を割いていることを実証することで、良い答えを得る機会が増えます。 –

答えて

1

これは再帰的に、Array#forEach()でVanilla.jsの提案です。

function getNames(a) { 
 
    this.push(a.name); 
 
    Array.isArray(a.childCategories) && a.childCategories.forEach(getNames, this); 
 
} 
 

 
var data = [{ "name": "cA", "leaf": false, "largeIconId": null, "label": "cA", "hideAllSearchFilters": false, "guidePage": null, "expanded": false, "defaultSearchCategory": false, "childCategories": [{ "name": "cA-A", "leaf": false, "largeIconId": null, "label": "cA-A", "hideAllSearchFilters": false, "guidePage": null, "expanded": false, "defaultSearchCategory": false, "childCategories": [{ "name": "cA-A-A", "leaf": false, "largeIconId": null, "label": "cA-A-A", "hideAllSearchFilters": false, "guidePage": null, "expanded": false, "defaultSearchCategory": false, "childCategories": [{ "name": "cA-A-A-A", "leaf": false, "largeIconId": null, "label": "cA-A-A-A", "hideAllSearchFilters": false, "guidePage": null, "expanded": false, "defaultSearchCategory": false, "childCategories": [{ "name": "cA-A-A-A-A", "leaf": true, "largeIconId": null, "label": "cA-A-A-A-A", "hideAllSearchFilters": false, "guidePage": null, "expanded": false, "defaultSearchCategory": false, "childCategories": [] }] }] }] }, { "name": "cA-B", "leaf": true, "largeIconId": null, "label": "cA-B", "hideAllSearchFilters": false, "guidePage": null, "expanded": false, "defaultSearchCategory": false, "childCategories": [] }, { "name": "cA-C", "leaf": true, "largeIconId": null, "label": "cA-C", "hideAllSearchFilters": false, "guidePage": null, "expanded": false, "defaultSearchCategory": false, "childCategories": [] }] }, { "name": "A", "leaf": false, "largeIconId": null, "label": "A", "hideAllSearchFilters": false, "guidePage": null, "expanded": false, "defaultSearchCategory": false, "childCategories": [{ "name": "A-Level1", "leaf": false, "largeIconId": null, "label": "A-Level1", "hideAllSearchFilters": false, "guidePage": null, "expanded": false, "defaultSearchCategory": false, "childCategories": [{ "name": "A-Level2", "leaf": true, "largeIconId": null, "label": "A-Level2", "hideAllSearchFilters": false, "guidePage": null, "expanded": false, "defaultSearchCategory": false, "childCategories": [] }] }] }], 
 
    names = []; 
 

 
data.forEach(getNames, names); 
 
document.write('<pre>' + JSON.stringify(names, 0, 4) + '</pre>');

関連する問題