Sometimes, we want to override nested property with JavaScript Object.assign.
In this article, we’ll look at how to override nested property with JavaScript Object.assign.
How to override nested property with JavaScript Object.assign?
To override nested property with JavaScript Object.assign, we can use the spread operator.
For instance, we write
const b = Object.assign({}, a, {
user: {
...a.user,
groups: "some changed value",
},
});
to call Object.assign
with an empty object a
, and a new object with the properties that override a
‘s properties to return object b
with the overridden nested propeties.
Conclusion
To override nested property with JavaScript Object.assign, we can use the spread operator.