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

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -