0

Problem when binding values into a nested object

I want to add data to the following object structure.

Company {
    stat {
        internalData {
            value = 35
        }
    }

}

I have used the below code

Company.stat.internalData["value"] = 35;

When I used the above code I got the error as internalData is undefined. Could someone kindly help me. Thanks inadvance.

2 Answers 2

1

It seems that you have already created the following object:

let Company = {
    stat: {

    }
}

As you can see, internalData is sure not defined. You can't access it's fields using internalData["value"]. You need to create it first like this:

Company.stat.internalData = {};

And then define its property called value:

Company.stat.internalData["value"] = 35;

Company.stat.internalData.value = 35; would work too.

Or, you can create the entire object with just one expression:

let Company = {
    stat: {
        internalData: {
            value: 35
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Issue here is you are trying to access the property before the object Company is defined. So you need to define something like this before setting the value,

Company = {};
Company.stat = {};
Company.stat.internalData["value"] = 35;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.