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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:42:29+00:00 2026-05-26T18:42:29+00:00

I have an object and one of the properties is an array. I want

  • 0

I have an object and one of the properties is an array. I want to set a new property on that array (customProp = 0). However, I want to do so inside myObject’s declaration.

myObject = {
  someString: "text",
  someArray: ["one","two"],
    /* What I've Tried */
    someArray: { customProp: 0 } // Overwrites my array with a new object
    someArray["customProp"]: 0 // Invalid syntax
    someArray: customProp: 0 // Also invalid
}

If I can’t create the array then set a property on it, can I do so in one fell swoop (again, staying within the confines of this object)?



I have another (small) question: How can I reference one of the properties still inside the declaration. I want to set otherProp = someString, how would I do so?

myObject = {
  someString: "text",
  otherString: someString, // someString is undefined
  otherString: myObject["someString"], // myObject is undefined
  otherString: this["someString"] // Just... undefined
}

I may need to split this into a separate question, but hopefully whoever answers the first will know the answer to the second.

  • 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-26T18:42:29+00:00Added an answer on May 26, 2026 at 6:42 pm

    Unfortunately neither of your requests are possible. Object literals in JavaScript are convenient but come with drawbacks, mostly what you’ve discovered.

    When you are inside an object literal, the object doesn’t quite exist yet. The JavaScript interpreter hasn’t finished ingesting it. So inside the literal, this points to the object just outside the literal, and the literal has no way to refer to other parts of itself.

    Fortunately, you can do both of what you want by just doing it after the literal declaration.

    myObject = {
        someString: 'text',
        someArray: ['one', 'two']
    };
    
    myObject.someArray.customProp = 0;
    myObject.otherString = myObject.someString;
    

    Or if you want, you can wrap all of this inside a constructor function and create your object with new.

    function MyObject() {
        this.someArray = ['one', 'two'];
        this.someArray.otherProp = 0;
        this.otherString = this.someString = 'text';
    }
    
    var myObject = new MyObject();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object MyObject and one of its properties ItemIDs is an array
Say i have a object in dynamic memory (new) and inside one of its
Basically I have some class objects, each with three properties. Once one class object
I have an object called Document that has a one-to-many relationship with DocumentPersonCc. I
I have an object whose value may be one of several array types like
I have the following code which copies property values from one object to another
I have a TreeView showing an object's properties. One of the properties is a
I have a JSON-array containing objects of different types with different properties. One of
I have an array of object names. These objects are HTML elements. I want
I have a VB class inside which one of the methods accepts an array

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.