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 121387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:57:46+00:00 2026-05-11T03:57:46+00:00

I have an associative array (object) of the following construction var menu = new

  • 0

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.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T03:57:47+00:00Added an answer on May 11, 2026 at 3:57 am

    To create a new object with no properties, use:

    var menu = new Object(); 

    The Object constructor actually takes one optional argument. Now let’s look at what you have:

    var menu = new Object('Submenu A','Submenu B','Submenu C','Submenu D'); 

    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 new String object being returned instead of an Object object. I’m speculating that, that may be reason Prototype’s each() 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:

    var menu = {     'Submenu A': {         'Option A': 'foo blah text',         'Option B': 'blah more text'     },     'Submenu B': {         'Option A': 'blah blah',         'Option B': 'texty text'     } }; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an associative array assigned to a Smarty variable, something like this: $foo
I have a simple associative array: $ar = array( 1=>'foo', 2=>'bar', 5=>'foobar', 8=>'barfoo' )
I have a nested associative array like this: $inputTypes= array( natural => array( text,
I have a PHP array of associative arrays with the following format: array(1) {
Namely, how does the following code: var sup = new Array(5); sup[0] = 'z3ero';
I have an associative array with two object inside. Running this through $(myassoc).each() ,
I have the following result in AMFPHP from a sql query: (Array)#0 [0] (Object)#1
I have a JavaScript object that is treated as an associative array. Let's call
Say I have the following: var a = '1', b = 'foo'; How do
I've been using Object as a way to have a generic associative array (map/dictionary)

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.