I wrote a post a couple of weeks back about the spread operator.
5 Uses for the Spread Operator
Lauri...
For further actions, you may consider blocking this person and/or reporting abuse
Hi! Thank you for writing this!
I came across some unexpected results the other day. It may be worth sharing:
Oh interesting. I just played around with this a bit. And found this.
So it makes sense that the spread syntax would copy that same array.
According to the spec, forEach elides missing array items, so all of those undefined elements won't be accounted for. Why it doesn't in the final case I don't know. Will have to come back and look into a bit.
Thanks for the example!
I just didn't know that
arr.slice()
and[...arr]
are not equivalent.Here's another example, without
forEach
.arr.slice()
vs[...arr]
So this is what I see
But no docs are telling me why that's the case. Still searching because I genuinely want to know!
So the difference is holes in an array versus undefined elements. And that happens due to this:
Also explained this way:
Awesome! Thank you!
This is a great write up! Thank you for sharing. I do have a question though.
I don’t understand the example about nested objects; why is
copy
in the end still the same as the originalobj
? I feel that with the accompanying explanation maybe the snippet is wrong, but I don’t know much about how these details work; would you mind explaining this one a bit further?I wrote out a full explanation and the realized the snippet is indeed wrong! Thanks for catching that.
Thanks for clearing it out!
cursed code
source dassur.ma/things/deep-copy/
Indeed. Stringify causes all kinds off problems with Dates.
Very interesting and helpful explanation. Thanks!
So glad it was helpful!
Great read, this stuff is really handy to know when testing with something like Jest as well ..
You're correct! But for the examples I put in the post it's a good option. Nice to have these additional links as well.