+1 vote
by (210 points)

Hey,

I'm sure this is absolutely basic question, but I still haven't found the answer. Let's say I have something like this:

<<set $Currentclothing = {
	"top" : "shirt",
	"topstyle" : "sporty",
	"bottom" : "pants",
	"bottomstyle" : "sporty"
}>>

and

<<set $Jumper = {
	"top" : "jumper",
	"topstyle" : "casual",
}>>

The problem is when I do <<set $Currentclothing to $Jumper>>, it deletes the "bottom" and "bottomstyle" properties, while I need it to stay intact. Is there any way to do it?

Btw of course I know I can simply do <<set $Currentclothing.top to "jumper">> etc., this is just an example.

 

Thanks a lot!

1 Answer

+1 vote
by (68.6k points)

There are a few ways to do it, but the cleanest is probably Object.assign().  For example:

<<set $Currentclothing = Object.assign($Currentclothing, $Jumper)>>

You don't actually need to reassign to $Currentclothing with that setup, but I like it as a pattern.  What I mean is that the following would also work in this case:

<<run Object.assign($Currentclothing, $Jumper)>>

 

by (210 points)
Amazing, that's exactly what I needed. Thx a lot for quick answer.
...