My question is simple, I have 2 objects like this:
object1 = {
content1: {}
}
object2 = {
stuff: {},
moreStuff: {}
}
And I want to add the content of object2 to content1 (which is inside of object1 ).
Like this:
object1 = {
content1: {
stuff: {},
moreStuff: {}
}
}
这很简单;
object1.content1 = object2
This will allow you to add an object inside another object.
With other examples you will get a substitution instead of adding . eg
const obj1 = { innerObj:{ name:'Bob' }, innerOBj2:{ color:'blue' } } const obj2 = { lastName:'Some', age:45 } obj1.innerObj = Object.assign(obj1.innerObj,obj2); console.log(obj1);
Now if you need something more advance, you should take a look to some functional programming framework like ramda , which will allow you to merge object. R.merge .
阻止你做的事: object1.content1 = object2
?
尝试这个
object1.content1 = object2;
Initialization
var object1 = {
content1:"1"
}
var object2 = {
content2:"2",
content3:"3"
}
Put contents of object2 into object1's content1
object1.content1 = object2;//this is the code you are looking for
console.log(object2);
You are done!
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.