Search code examples
javascriptobject-literal

How to add object literal within object literal


Assuming I have an object literal that looks like this:

C = {"A":"a","B":"b","C":"c"};

and I want to add another object like this..

"D" : {"E":e}

where e is a variable. and e = "ValueOfE" So that C will have this kind of value..

C = {"A":"a","B":"b","C":"c", "D" : { "E" : "ValueOfE"}};

How will I do that?


Solution

  • The values in object literals can be arbitrary expressions, which means that they can contain other object literals, make use of existing variables, and so on; so if you're asking what I think you are, then you can write:

    C = { "A": "a", "B": "b", "C": "c", "D": { "E": e } };