Categories
JavaScript Answers

How to destructure onto an existing object with JavaScript?

Spread the love

Sometimes, we want to destructure onto an existing object with JavaScript.

In this article, we’ll look at how to destructure onto an existing object with JavaScript.

How to destructure onto an existing object with JavaScript?

To destructure onto an existing object with JavaScript, we can destructure the properties into variables and then put the properties into the new object.

For instance, we write

const { prop1, prop2, prop3 } = someObject;
const data = { prop1, prop2, prop3 };

to destructure the prop1, prop2 and prop3 properties from someObject.

Then we put the values of each variable as properties of the data object.

Conclusion

To destructure onto an existing object with JavaScript, we can destructure the properties into variables and then put the properties into the new object.

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 *