javascript - New array overwriting global array -
javascript - New array overwriting global array -
i have global array like:
var myarray = [];
and has various elements in it. need create new array same contents , reverse this:
newarray = myarray; newarray = newarray.reverse();
however when this, reverses both myarray , newarray.
what doing wrong?
thanks!
it's because both arrays referenced same object. rid of have clone slice..
var myarray = [1,2]; var newarray = myarray.slice(0) newarray.reverse();
javascript
Comments
Post a Comment