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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:50:19+00:00 2026-06-09T13:50:19+00:00

Say i have this object: test = { testObj: { 1: { key: value

  • 0

Say i have this object:

test = {
    testObj: {
        "1": {
            "key": "value"
        }
    }
}

And i want to add values to testObj like so:

test.testObj["2"].key = "my value";

I get error TypeError: Cannot set property 'key' of undefined

Now i do understand that key does not exist yet, but 2 doesn’t exist also, and yet i can set value to it:

test.testObj["2"] = "something";

So what can i do about it?

EDIT
wow i feel stupid for not figuring that out by myself… anyways thank you guys.

  • 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-09T13:50:21+00:00Added an answer on June 9, 2026 at 1:50 pm

    Javascript doesn’t know what test.testObj["2"] should be in this scenario, so it ends up testing it as an existing property:

    test.testObj["2"].key = "my value";
    

    The assignment can only apply to the last part of the structure on the left.

    But you can tell it what it is by creating the object first:

    test.testObj["2"]={};
    test.testObj["2"].key = "my value";
    

    Or in a single step:

    test.testObj["2"] = { key: "my value"};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a class like this: class Test(object): prop = property(lambda self:
Say I have an object-like data record like this: $article = array( 'title' =>
This is a best practices question Let say, I have a class object, like
Let's say that I have a object that looks like this var object =
Say I have a compound JSON object like so: { Person: { name:test, age:
Say I have a class like this: public class Test { public class InnerTest{}
This one is a little tricky. Say I have this XmlDocument <Object> <Property1>1</Property1> <Property2>2</Property2>
I have this huge domain object(say parent) which contains other domain objects. It takes
Let's say I have an object - a User object in this case -
I have this in my test Project.should_receive(:find).with(@project).and_return(@project) but when object receive that method call

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.