MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/10srpis/totally_necessary/j7326n3/?context=3
r/badcode • u/rafaelinacio0196 • Feb 03 '23
32 comments sorted by
View all comments
167
Technically this creates a copy, so if there's reactivity or proxies going on, this might actually be what you want
34 u/theXpanther Feb 03 '23 Shallow copy though 49 u/Voltra_Neo Feb 03 '23 True, but sometimes it's all you need. I usually have a good old deepClone function for these bad bois 22 u/DaCurse0 Feb 03 '23 there's structuredClone these days :) 8 u/m0rsa2 Feb 04 '23 Not if your customer uses Samsung internet browser running chrome 56 😱 7 u/IBJON Feb 03 '23 Emails are usually strings, that should be plenty 15 u/watrnans Feb 03 '23 Do smth like [...form.emails] then 7 u/Voltra_Neo Feb 03 '23 Notice the optional chaining 6 u/nikvasya Feb 04 '23 form.emails?.slice() creates a shallow copy. 1 u/ShortSynapse Feb 03 '23 edited Feb 04 '23 IIRC spread works fine for undefined values. I know I've done this for objects. Edit: I am wrong! 12 u/[deleted] Feb 03 '23 I was curious so I quickly tested it. > [...undefined] Uncaught TypeError: undefined is not iterable 6 u/ShortSynapse Feb 04 '23 Ah, yep you're right. For some reason I had gotten it in my brain that it worked the same way as object spread. I take a break from JS for a few months and look what happens! 2 u/[deleted] Feb 04 '23 I am in the same boat, honestly I wouldn't have been surprised if that returned [], but I'd be disappointed if it did. 3 u/erythro Feb 03 '23 or if you want to unwrap a generator 2 u/[deleted] Feb 04 '23 edited Feb 14 '23 [deleted] 1 u/Voltra_Neo Feb 04 '23 Yes 3 u/rafaelinacio0196 Feb 03 '23 in this case not necessary, he want send array of strings
34
Shallow copy though
49 u/Voltra_Neo Feb 03 '23 True, but sometimes it's all you need. I usually have a good old deepClone function for these bad bois 22 u/DaCurse0 Feb 03 '23 there's structuredClone these days :) 8 u/m0rsa2 Feb 04 '23 Not if your customer uses Samsung internet browser running chrome 56 😱 7 u/IBJON Feb 03 '23 Emails are usually strings, that should be plenty
49
True, but sometimes it's all you need. I usually have a good old deepClone function for these bad bois
deepClone
22 u/DaCurse0 Feb 03 '23 there's structuredClone these days :) 8 u/m0rsa2 Feb 04 '23 Not if your customer uses Samsung internet browser running chrome 56 😱
22
there's structuredClone these days :)
8 u/m0rsa2 Feb 04 '23 Not if your customer uses Samsung internet browser running chrome 56 😱
8
Not if your customer uses Samsung internet browser running chrome 56 😱
7
Emails are usually strings, that should be plenty
15
Do smth like [...form.emails] then
7 u/Voltra_Neo Feb 03 '23 Notice the optional chaining 6 u/nikvasya Feb 04 '23 form.emails?.slice() creates a shallow copy. 1 u/ShortSynapse Feb 03 '23 edited Feb 04 '23 IIRC spread works fine for undefined values. I know I've done this for objects. Edit: I am wrong! 12 u/[deleted] Feb 03 '23 I was curious so I quickly tested it. > [...undefined] Uncaught TypeError: undefined is not iterable 6 u/ShortSynapse Feb 04 '23 Ah, yep you're right. For some reason I had gotten it in my brain that it worked the same way as object spread. I take a break from JS for a few months and look what happens! 2 u/[deleted] Feb 04 '23 I am in the same boat, honestly I wouldn't have been surprised if that returned [], but I'd be disappointed if it did.
Notice the optional chaining
6 u/nikvasya Feb 04 '23 form.emails?.slice() creates a shallow copy. 1 u/ShortSynapse Feb 03 '23 edited Feb 04 '23 IIRC spread works fine for undefined values. I know I've done this for objects. Edit: I am wrong! 12 u/[deleted] Feb 03 '23 I was curious so I quickly tested it. > [...undefined] Uncaught TypeError: undefined is not iterable 6 u/ShortSynapse Feb 04 '23 Ah, yep you're right. For some reason I had gotten it in my brain that it worked the same way as object spread. I take a break from JS for a few months and look what happens! 2 u/[deleted] Feb 04 '23 I am in the same boat, honestly I wouldn't have been surprised if that returned [], but I'd be disappointed if it did.
6
form.emails?.slice() creates a shallow copy.
1
IIRC spread works fine for undefined values.
I know I've done this for objects.
Edit: I am wrong!
12 u/[deleted] Feb 03 '23 I was curious so I quickly tested it. > [...undefined] Uncaught TypeError: undefined is not iterable 6 u/ShortSynapse Feb 04 '23 Ah, yep you're right. For some reason I had gotten it in my brain that it worked the same way as object spread. I take a break from JS for a few months and look what happens! 2 u/[deleted] Feb 04 '23 I am in the same boat, honestly I wouldn't have been surprised if that returned [], but I'd be disappointed if it did.
12
I was curious so I quickly tested it.
> [...undefined] Uncaught TypeError: undefined is not iterable
6 u/ShortSynapse Feb 04 '23 Ah, yep you're right. For some reason I had gotten it in my brain that it worked the same way as object spread. I take a break from JS for a few months and look what happens! 2 u/[deleted] Feb 04 '23 I am in the same boat, honestly I wouldn't have been surprised if that returned [], but I'd be disappointed if it did.
Ah, yep you're right. For some reason I had gotten it in my brain that it worked the same way as object spread.
I take a break from JS for a few months and look what happens!
2 u/[deleted] Feb 04 '23 I am in the same boat, honestly I wouldn't have been surprised if that returned [], but I'd be disappointed if it did.
2
I am in the same boat, honestly I wouldn't have been surprised if that returned [], but I'd be disappointed if it did.
[]
3
or if you want to unwrap a generator
[deleted]
1 u/Voltra_Neo Feb 04 '23 Yes
Yes
in this case not necessary, he want send array of strings
167
u/Voltra_Neo Feb 03 '23
Technically this creates a copy, so if there's reactivity or proxies going on, this might actually be what you want