2

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

2
  • But element is duplicated many times... Commented Nov 6, 2012 at 8:08
  • yes but it has different children and name and some other properties , I want to walk through all the 'element' objects Commented Nov 6, 2012 at 8:10

1 Answer 1

3

So just from running JSLint in your jsfiddle page I found a few things:

  1. An object has a number of keys and values, but how would you differentiate between different values if they had the same keys? In other words, you cannot have multiple keys with the same name, so your nested object with multiple "element" keys will be invalid.
  2. I don't think you can declare element inside the for loop, try declaring it ahead of time:

    var element; for (element in configs) {

  3. 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:

var config = {
    "tree": {
        "element": {
            "name": "pd",
            "children": {
                "element1": {
                    "name": "pd2",
                    "children": {}
                },
                "element2": {
                    "name": "pd3",
                    "children": {
                        "element1": {
                            "name": "pd6",
                            "children": {}
                        },
                        "element2": {
                            "name": "pd5",
                            "children": {
                                "element": {
                                    "name": "pd7",
                                    "children": {
                                        "element": {
                                            "name": "pd8",
                                            "children": {}
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                "element3": {
                    "name": "pd4",
                    "children": {}
                }
            },
            "element": {
                "name": "pd4",
                "children": {}
            }
        }
    }
};

function parseConfig(configs) {
    var element;
    for (element in configs) {
        if (typeof(configs[element]) == "object") {
            console.log(configs[element]);
            parseConfig(configs[element]);
        }
    }
}
parseConfig(config);​

An alternative way to having multiple "element" keys, would be to have an "elements" array which contains a list of elements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.