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.