I have the following example code.
var myObj1 = {
name: {
first: "First",
last: "Last"
}
};
var myObj2 = {
name: {
first: "Other",
middle: "Middle"
}
};
var myMainObj = {
...myObj1,
...myObj2
};
console.log(myMainObj);
I expect myMainObj to be the following:
{
name: {
first: "Other",
middle: "Middle",
last: "Last"
}
}
But myMainObj ends up being:
{
name: {
first: "Other",
middle: "Middle"
}
}
So I have two objects. I want to basically combine them, and have the ability to assign priority to one of the objects over another.
Both objects come from external sources, so the structure and data is not guaranteed, and I have to account for the data being in different structure.
How can I do this easily since the spread operator only works for the root level of the object?
I am looking for the cleanest and easiest solution to do this.