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

c# - ASP.NET MVC Sequence contains no matching element -

java - Parsing XML, skip certain tags -

rest - How to invalidate user session on inactivity in a stateless server? -