If you want to update a piece of an object while creating a new object, then the spread syntax is the way to go.
When you see...
, you should just think of flattening the object.
When you see this:
constmetadata={first:"Matt",last:"Crowder"};constupdatedMetadata={...metadata,last:"Jenkins"};
You should just seeupdatedMetadata
as such:
constupdatedMetadata={first:"Matt",last:"Crowder",last:"Jenkins"};
Key/value assignments are read from top to bottom, so the last key's value will take priority in setting the value.
And nowupdatedMetadata
will have the value of:
{first:"Matt",last:"Jenkins"}
So the spread flattens out the object wherever you place it, so if we had placed the spread metadataafterlast: "Jenkins"
, then we would get no updates!
constmetadata={first:"Matt",last:"Crowder"};constupdatedMetadata={last:"Jenkins",...metadata};// results inconstupdatedMetadata={last:"Jenkins",first:"Matt",last:"Crowder"};// which gives us nothing
So be careful where you place your spread syntax!
Top comments(1)
For further actions, you may consider blocking this person and/orreporting abuse