I had this experience when I was just beginning to understand javascript, at the same time I was exploring Vue. I got into situation where I assigned a variable to a spliced array, and I was so dumb wondering why does both arrays are changing even if I just manipulated one of them.
I was googling for answers but I wasn't able to see a solution - soooo I ended up using Json Stringify and Parse. When I assign the array to a variable, I, first, Stringify it and then Parse it (just to make a "shallow" copy). Yea, funny right? Months later I accidentally found out the solution of the problem while reading some article. That "months later" is today, and the "article" is yours. Thanks a lot. .slice() FTW! \m/
Software dev at Netflix | DC techie | Conference speaker | egghead Instructor | TC39 Educators Committee | Girls Who Code Facilitator | Board game geek | @laurieontech on twitter
I had this experience when I was just beginning to understand javascript, at the same time I was exploring Vue. I got into situation where I assigned a variable to a spliced array, and I was so dumb wondering why does both arrays are changing even if I just manipulated one of them.
I was googling for answers but I wasn't able to see a solution - soooo I ended up using Json Stringify and Parse. When I assign the array to a variable, I, first, Stringify it and then Parse it (just to make a "shallow" copy). Yea, funny right? Months later I accidentally found out the solution of the problem while reading some article. That "months later" is today, and the "article" is yours. Thanks a lot. .slice() FTW! \m/
So glad it helped! Sorry it came a little late 😅
It's perfect. Thaaaaanks.