I am trying to walk throught this tree and print all the “element” objects but its not working with me
var config = {
"tree": {
"element": {
"name": "pd",
"children": {
"element": {
"name": "pd2",
"children": {}
},
"element": {
"name": "pd3",
"children": {
"element": {
"name": "pd6",
"children": {}
},
"element": {
"name": "pd5",
"children": {
"element": {
"name": "pd7",
"children": {
"element": {
"name": "pd8",
"children": {}
}
}
}
}
}
}
},
"element": {
"name": "pd4",
"children": {}
}
}
}
}
}
but it only print two objects multiple times
here is my code
function parseConfig(configs){
for(var element in configs){
if (typeof(configs[element])=="object") {
console.log(configs[element]);
parseConfig(configs[element]);
}
}
}
and here is the code on jsfiddle
So just from running JSLint in your jsfiddle page I found a few things:
I don’t think you can declare element inside the for loop, try declaring it ahead of time:
var element;
for (element in configs) {
If you’re actually expecting this code to do something in this jsfiddle, you’re going to have to put it in an onLoad function. If you’re just using jsfiddle to show us your code, then I’m hoping you’re actually calling it wherever you’re using it.
I numerated your keys and fixed #2 here. Or see it below here:
An alternative way to having multiple “element” keys, would be to have an “elements” array which contains a list of elements.