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

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

ubuntu - Bash Script to Check That Files Are Being Created -