Closed ghost closed 9 years ago
When you assign cardDeck to tempArray, you are making a reference to the Array. Any changes to either are seen by both. You would have to clone cardDeck, so that any changes you make to the clone do not affect the original.
Yep! This is a weird thing about Javascript (and most programming languages). It's not making a copy of the array; it's making a reference to the object that already exists.
The easiest way to clone an array is with var arrayCopy = myArray.slice()
.
Thank you both! I think I'm headed in the right direction now
It may be a good idea to avoid delete
, since this can cause some wacky things with arrays (particularly when you try to do stuff with array.length
). A better bet may be .splice()
(not to be confused with .splice()
... I know, I know).
I tried...
where cardQuestion is a variable defined as a key in cardDeck.
expecting to delete a key from tempArray object but leaving it in the global object however that line of seems to remove the key pair from both the objects?