Here are 6 ways to use the Spread operator with Array in JavaScript. You can use it to merge or clone an array. Or use it to convert iterables to...
For further actions, you may consider blocking this person and/or reporting abuse
Side note: The
new Set()
and thespread
operator could be quite dangerous, worth to check out the behaviour: it is really copy by value or just by reference... (might be handy this small detail when ppl workin' on large datasets)But wouldn't
spread
be creating a true new copy? -- do you mind expanding your thought, might be something helpful to include in my code notes 😵Try this:
The conclusion is: spreading results in a shallow copy.
Source: stackoverflow.com/questions/500511...
That means, copying primitive values turns fine. What you can do in case of objects copy, is to make a copy of those underlying objects:
But how about nested objects? Try this:
The conclusion is:
Object.assign
copies property values only (not a deep copy).Source: developer.mozilla.org/en-US/docs/W...
Conclusions:
- spreading results in a shallow copy
-
Object.assign
copies property values only (not a deep copy)I loved reading this article!! No bs, right to the point. And great analogies with Russian dolls or Eraser! I didn't understand that mud from MDN as well :D
Although it is a familiar topic to me, I found it entertaining and interesting enough to read it till the end. Great work! :-)
Who said learning programming can't be entertaining or interesting 😆 Thanks so much for the positive feedback! Entertain-learning (yup, I just TM that 😂) is my goal for all my code notes now! 🙌
Fun! I can see a somewhat unusual use for this: in web games, you need arrays for various reasons. It’s usually more efficient to store only one dimension, even if the data isn’t, and simulate a multidimensional array. Spread gives a convenient way to make the simulation less annoying for consumers, from the perspective of a JS library.
Interesting, thanks for sharing! 🎮
Nice post,
Thanks, Samantha
Thank for reading my code notes 😄
Lovely useful aericle.
I also often use it to get min and max of an array 😊
min and max? please say more! 🤩
Well, the min() and max() method cannot take an array as an input, so spreading it can solve that problem too... It saved me a lot of headaches.
Ah yes! that's a fantastic use case! let me add it to my notes, thanks for sharing 💪
Samantha! You just make everything simple and easy to understand. Please try to share more content. I have been addicted to yours 😭
Thank you so much!!! Will dooo 😊😊😊
Nice to read! Thank you!
Yay! thank you for reading 🙂
Good explanation.
thanks, glad you find it helpful 👏
I dont use spread operators much but now that I have a better understanding on how it work i'd probably use them more. Thanks for sharing this.
great! that's the goal of the post. Spread is super awesome, but it can be tricky to understand. So glad it makes more sense to you and you're encouraged to use it more 👏
I like reading your articles 🤸.
Awesome happy to hear that! glad you find them helpful 👏
Thank you for sharing this, now I think I understand spread operator!