Question:
I am trying to adddata from another object into a new object while also adding new dataconst addCardData = { ...optIn, cardId, cardName}
here optIn already has cardId and cardName so they are getting replaced by new values, how to make an addCardData such that cardId and cardName not be replaced but added to the object or as a sub object? or is there any other better way to do this?Answer:
If your goal is to keep the values from “optIn”, you could add them last.For example:
If your goal is to add both versions of cardId and both versions of cardName, you have two options:
- Change the name of cardId and cardName, since an object can only old one attribute with a given name.
For example:
- Add the optIn object as an attribute
addCardData.optIn.cardId
.If you have better answer, please add a comment about this, thank you!