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

java Multi query from Mysql using netbeans -

c# - DotNetZip fails with "stream does not support seek operations" -

c++ - StartServiceCtrlDispatcher don't can access 1063 error -