recursively loop through nested JSON
HTML
<div id="list"></div>
JavaScript
var allData = {
"Home": {
"Home_Sub": ["home1", "home2", "home3"]
},
"About": ["about1", "about2", "about3"],
"Contact": ["contact1", "contact2", "contact3"]
};
function printList(items) {
switch ($.type(items)) {
case "object":
getChildren(items);
break;
case "string":
console.log(items);
break;
case "array":
printArray(items);
break;
}
}
function getChildren(parent) {
for (var child in parent) {
console.log(child);
printList(parent[child]);
}
}
function printArray(myArray){
for(var i = 0; i < myArray.length; i++){
console.log(myArray[i]);
}
}
printList(allData);