Hello! I’m doing the changeMachine project in JS. I want to take “cid” array from the arguments and work with it, but without changing it (I need it later). Then I create two new arrays (originalCid and cidMutable) using cid.slice() method. I thought that after using this method, I would be able to modify the new arrys with no mutation of the original one, but that’s not happening. Can anybody say me why?
please, don’t post screenshots, copy and paste your code (with proper formatting) or use a repl.it or jsfiddle or codepen or whatever you want
I think the thing is that you have made a copy of the array cid but each array inside the copy is still a reference to those arrays inside cid
I copied a multidimensional array with slice like you did, and this is what happen (the same happen with the spread operator, so @oristar2018 suggestion will still have the same effect) :
Thanks! I will post my code next time, I didn’t thought it’s easyer to replicate the problem if all the code is posted!
This comment: I think the thing is that you have made a copy of the array cid but each array inside the copy is still a reference to those arrays inside cid
Oh my God, it’s really weird… I woult have never thought that the problem was that one… I have found this topic talking about it: