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

  • Home
  • SEARCH
  • 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 7571411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:40:10+00:00 2026-05-30T15:40:10+00:00

I have an object within an object. It looks like this. var myLib =

  • 0

I have an object within an object. It looks like this.

 var myLib = {
     object1: {}
 }

My basic problem is that I wanted to end up like this. So I would like to do this dynamically I will not know the property’s or additional objects until run time.

var myLib = {
    object1: ({"A1":({"Color":"Blue",
                      "height":50})
    })
}

From reading here on Stack Overflow I know that I can create an object within an object by simply going like this:

  myLib.Object1["A1"] = "Something"

But this does not produce what I’m looking for.

I tried this syntax which I know is wrong but basically

 mylib.Object1["A1"].["color"]="Blue";

so basically here is the question. I would like to create object “A1” under “mylib.Object” and immediately add property color = “blue” to “A1”. I would need to do this for several other properties, but if I can figure out how to do this for one, I can figure it out for the rest. How can I accomplish this task?

No jQuery, please. Just plain old JavaScript is what I’m looking for.**

Once I create the object and properties I would imagine I can just use a for loop to loop through the properties for that object. Like so:

for(key in mylib.Object1["A1"]){}

Right?

  • 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-05-30T15:40:11+00:00Added an answer on May 30, 2026 at 3:40 pm

    You can create it all from scratch like this:

    var myLib = {};
    myLib.object1 = {};
    
    // assuming you get this value from your code somewhere
    var x = "A1";   
    
    myLib.object1[x] = {Color: "Blue", height: 50};
    

    Or, if all values are in variables:

    var myLib = {};
    myLib.object1 = {};
    
    // assuming you get this value from your code somewhere
    var x = "A1";   
    
    var colorProp = "Color";
    var colorPropValue = "Blue";
    var heightProp = "height";
    var heightPropValue = 50;
    myLib.object1[x] = {};    // create empty object so we can then add properties to it
    myLib.object1[x][colorProp] = colorPropValue;   // add one property
    myLib.object1[x][heightProp] = heightPropValue; // add another property
    

    These syntaxes create identical results:

    myLib.object1.A1 = {};
    
    var x = "A1";
    myLib.object1[x] = {};
    

    The first can only be used when the property name is known when you write the code and when the property name follows the proper rules for a javascript identifier. The second can be used any time, but is typically used when the property name is in a variable or when it doesn’t follow the rules for a javascript identifier (like it starts with a digit).

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

Sidebar

Related Questions

I have an object that looks like this: var MyObject = { prop1 =
I have an item object that looks like this: item[siteId] = {7BAD6B24-3B69-4CB9-AD02-9B0003C1927D}; item[webId] =
Let's say I have a list that looks like this: <ul> <li id=q></li> <li
I have two repeaters that looks like this : Arrange By: Name | Age
I have an array of objects called objarray . Each object looks like this:
i have a method as follows. test(Object obj){ } now within this method i
I have a theoretical grid of overlapping rectangles that might look something like this:
I have an input form, nested within a div, defined like this: <div class=login-input>
I have a class member in an ActionScript 3 class that looks something like
I have a QVariant object within a QTreeWidgetItem, how can I cast it to

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.