Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8786177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:33:00+00:00 2026-06-13T21:33:00+00:00

I am trying to walk throught this tree and print all the element objects

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:33:01+00:00Added an answer on June 13, 2026 at 9:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am kinda new to all this but I am trying to make myself
I'm trying to walk a directory, retrieve all files within that directory ending with
I am just trying to walk through this tutorial http://jimneath.org/2011/03/24/using-redis-with-ruby-on-rails.html#redis_and_rails And when I put
I'm trying to follow this walk-through: Hosting a WCF in a Windows Service Up
I will try to walk you through this very slow. I am trying to
Hi I am trying to understand this algorithm and I and trying to walk
Am trying to walk through the log-cat , but as I scroll up -
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Trying to figure out how I can do this properly. The print_r looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.