0

I am building a jQuery plugin for a homework assignment. I am inserting an object into my plugin, as a parameter.

Basically, it looks like this:

$('elem').pluginName({
    structure: {
        firstButton: "home.html"
        secondButton: {
            "Submenu 1": "submenu1.html",
            "Submenu 2": "submenu2.html"
        }
        thirdButton: {
            "Submenu 1": {
                "Subsubmenu 1": "subsub1.html"
            }
        }
    }
});

I want a user to be able to enter any possible structure, so I want to be able to loop infinitely deep into the structure object. I don't know how to, I am hoping someone can help me out.

user3239713
  • 781
  • 2
  • 6
  • 11
  • Look http://stackoverflow.com/questions/8085004/iterate-through-nested-javascript-objects – Aamir Afridi Mar 28 '14 at 15:11
  • @AamirAfridi: I unfortunately do not see how that link would help me, as I want to account for infinite deep layers. – user3239713 Mar 28 '14 at 15:14
  • 1
    possible duplicate of [Traverse all the Nodes of a JSON Object Tree with JavaScript](http://stackoverflow.com/questions/722668/traverse-all-the-nodes-of-a-json-object-tree-with-javascript) – jfriend00 Mar 28 '14 at 15:14

1 Answers1

0

You should use jQuery's $.each() method. It will loop through the properties of an object. From the link:

objects are iterated via their named properties.

So you would do something like this:

$.each(structure, function(index, property){
    //do stuff here...
});

Hope that helps!

biddano
  • 491
  • 5
  • 16