Categories
JavaScript Answers

How to override nested property with JavaScript Object.assign?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.