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.
myObject.myChildObject = 'foo'
或者,如果密钥中有空格,则可以使用方括号表示法:
myObject['my Child Object'] = 'foo'
If You want to add a child to myObject then just do it
var myObject = {};
myObject.myChildObject = "Foo Bar"
var myObject = {};
myObject.myObject = Object.assign({myChildObject:"red"});
console.log(myObject);
Result:
{
"myObject ":{
"myChildObject":"FooBar"
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.