I have a global array like:
var myArray = [];
And it has various elements in it. I need to create a new array with the same contents and then reverse it like this:
newArray = myArray;
newArray = newArray.reverse();
However when I do this, it reverses both the myArray and newArray.
What am I doing wrong?
Thanks!
.sliceis enough to clone an array. If you have objects in array, you have to make a deep copy of array, andslicewill make only a shallow copy. If it's what you need then use.slicemethod.