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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *