I have an associative array (object) of the following construction
var menu = new Object('Submenu A','Submenu B','Submenu C','Submenu D'); menu['Submenu A']=new Object(); menu['Submenu A']['Option A']= 'foo blah text'; menu['Submenu A']['Option B']= 'blah more text'; menu['Submenu B']['Option A']= 'blah blah'; menu['Submenu B']['Option B']= 'texty text'; ... etc.
This is iterated over a for..in loop which breaks when using prototype due to extending the class with methods like toJSON() and camelise()
I’ve attempted using Prototype’s .each method however it errors out reporting that menu[‘Submenu A’] is undefined, it can’t seem to locate the options that are defined under it.
What is the proper way to iterate over an associative array in prototype?
Further clarification (thanks for the answers so far). I’m slowly migrating a project over to using prototype, but it has large amounts of code that aren’t prototype compatible yet. The code in question is in a library used by several other files. The code as is called by a function, initialize_menu and contains the code
for (var i=0; i < menu.length; i++) { populate_sub_menu(menu[i]) }
The submenu function is structured like this
function populate_sub_menu(subMenu){ for (var option in menu[subMenu]) { html+=menu[subMenu][option]+'html'+subMenu+option; } }
I’m leaving out code regarding getting html elements and such that aren’t related to the problem.
To create a new object with no properties, use:
The Object constructor actually takes one optional argument. Now let’s look at what you have:
Only the first argument (
'Submenu A') is being used while the others are simply ignored. However, passing a string literal to the Object constructor causes a newStringobject being returned instead of anObjectobject. I’m speculating that, that may be reason Prototype’seach()is acting up (if it’s really failing because of this).You mentioned that ‘it errors out reporting that menu[‘SubmenuA’] is undefined’. Does your question have a typo? You’re setting the ‘Submenu A’ property, with a space before the ‘A’, while the error you reported seems to lack the space.
You also seem to be taking the long route in setting the object properties. Using the object literal syntax will be a lot shorter and less error-prone: