Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add data to object

I am trying to adddata from another object into a new object while also adding new data
const 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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If your goal is to keep the values from "optIn", you could add them last.

For example:

addCardData =  { 
  cardId, 
  cardName, 
  ...optIn 
};

If your goal is to add both versions of cardId and both versions of cardName, you have two options:

  1. Change the name of cardId and cardName, since an object can only old one attribute with a given name.

For example:

addCardData =  { 
  newCardId: cardId, 
  newCardName: cardName, 
  ...optIn 
};
  1. Add the optIn object as an attribute
addCardData =  { 
  cardId, 
  cardName, 
  optIn
};

Then you can refer to it by addCardData.optIn.cardId.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading