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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:35:40+00:00 2026-06-17T11:35:40+00:00

I have a situation where I wish to build an array that can be

  • 0

I have a situation where I wish to build an array that can be accessed via:

myArray['Foo'].push(myObj);

But I do not know how to create it from scratch. From what I had read I thought maybe the following would do it:

var myArray = new Array(new Array());

Problem is that it only lets me use integers to reference the myArray[]. If I ignore this limitation and try the following it errors saying it doesn’t have .push():

myArray[i].push(myObj); 

I presume this is because myArray[i] is returning a string?

So my question is, how do I build a dynamic array where I reference the first dimension using strings and can then push and pop on the second dimension? Also if I take this approach, can I use push and pop for adding the string to the first dimension?


I thought about this a bit more before and wrote some code I thought may work, which it does.

var myArray = new Object();

var myObj = new Object();
var myObj2 = new Object();
var myObj3 = new Object();

myObj.name = "Harry";
myObj2.name = "Curly";
myObj3.name = "Moe";

myArray["first"] = new Array()
myArray["first"].push(myObj);
myArray["first"].push(myObj2);
myArray["first"].push(myObj3);

myArray["second"] = new Array()
myArray["second"].push(myObj2);

myArray["third"] = new Array()
myArray["third"].push(myObj3);

iterate(myArray, "first");
iterate(myArray, "second");
iterate(myArray, "third");

function iterate(array, name) {
    for(i = 0, l = myArray[name].length; i < l; i++) {
        console.log(" " + name + ": " + i + " value " + myArray[name][i].name);
    }
}

Is the above the correct way to go about it?


I’m sure I’ve got some terminology problems with the above, let me know and I’ll edit it so it’s correct.

  • 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-17T11:35:41+00:00Added an answer on June 17, 2026 at 11:35 am

    What you are calling myArray is not an array, it is an Object. Either of the following are acceptable:

    var myObject = {}
    var myObject = new Object();
    

    All of the following lines do the same thing; they create a key called “first” in the object “myObject” and store a new, empty array (into which you can push):

    myObject['first'] = new Array();
    myObject['first'] = [];
    myObject.first = new Array();
    myObject.first = [];
    

    Now you can push into this array:

    myArray["first"].push("hello");
    

    Here’s another way to write this from scratch:

    var myObject = {
        first: ["hello"]
    };
    

    So perhaps now the answers to your questions become more clear:

    How do I build a dynamic array where I reference the first dimension using strings and can then push and pop on the second dimension?

    By making the “first”, or outer dimension be an object rather than an array.

    Also if I take this approach, can I use push and pop for adding the string to the first dimension?

    Nope, but you can use

    myObject.somewhereNew = [];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying my hand with jQuery. I have this strange situation that i wish to
Can you not have code blocks in server tags? For example, I wish to
I have a situation where two separate companies wish to 'join' their iOS apps
I have situation, where running a query that filters by an indexed column in
I have situation where a user can manipulate a large set of data (presented
We have situation where say we have four engineers that are working on software
Let me start off by describing my situation. I have a main view that
The situation is as follows: I have a ViewModel that has a property that's
I wish I could have tested it myself, but unfortunately it's impossible to do
Assuming I have the following I wish to unit test: class Foo: IAMethods, IBMethods

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.