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": []
}]
}]
}];
答案 0 :(得分:1)
这是Vanilla.js中以递归方式Array#forEach()
提出的提案。
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>');