Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add child object with value to existing javaScript object

Tags:

javascript

I have an empty root object created in JavsScript:

var myObject = {};

Now, I want to add a child object with a value:

{  
   "myObject ":{  
      "myChildObject":"FooBar"
   }
}

Seems simple enough, but I'm having a hard time finding an answer. Most of the examples I find are related to DOM objects (div's) and don't really translate well. I've been looking at W3Schools on their .appendChild() page, but the examples there don't set a value.

like image 203
Casey Crookston Avatar asked Oct 26 '25 14:10

Casey Crookston


1 Answers

myObject.myChildObject = 'foo'

or, if your key has spaces in it you can use square bracket notation:

myObject['my Child Object'] = 'foo'

like image 160
Dominic Avatar answered Oct 28 '25 03:10

Dominic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!