views:

138

answers:

8

Here's some code that has two arrays(np and op), one a copy of the other

However, when I modify the copy, the original is also modified! take a look:

<script type="text/javascript">
var op=new Array(0, 0);
var np=op;
np[1]=2;
document.write(op+"<br>")
document.write(np)
</script>

Is there any way to retain the original and modify the copy?

+4  A: 

You never made a copy of the array. You simply assigned the array to another variable. This does not copy in Javascript. In your example there is only one array with two variables by which you can access it. There is no built-in way to copy arrays in Javascript so you will need to write your own function to do it.

Take a look at this StackOverflow question for a hint on how to actually implement the copying of the elements in the array.

Marc W
wow, had no idea, thanks!
pop850
Not absolutely necessary, if using an Array see my simpler example below. For an Object you will need to use the solution referenced.
David Caunt
The question you link to does not provide a solution for copying arrays.
J-P
@J-P: I said "copying the elements in the array", just in case he tries to do this in the future with JS objects. Posterity's sake, you see.
Marc W
+6  A: 

Some of the built in Array functions will actually create a copy for you. One such is slice.

For example:

var op=new Array(0, 0);
var np= op.slice(0);
np[1]=2;
document.write(op+"<br>")
document.write(np)

Reference http://my.opera.com/GreyWyvern/blog/show.dml/1725165

David Caunt
+1  A: 

You should clone the second array instead of copying it.

--- Update

If you assign an object to a variable, only the reference is copied (that means, they both point to the same data). For having an independent copy of this object you need to clone it. And there are several ways how to this, for example here is the way of cloning object using jQuery.

Igor Pavelek
Oh come on. That's like saying "you should do the right thing" without saying what the right thing is.
conny
I don't even understand this answer. @Igor, could you clarify?
Nosredna
Yes, sorry for being brief, Nosredna, I updated my answer.
Igor Pavelek
Thanks. Upvoted for update.
Nosredna
+2  A: 
simon
+3  A: 

What you are doing is not creating a copy of the array, it's only creating a copy of the reference to the array, so you get two references to the same array object.

This is how you can create an actual copy of the array:

var np = op.concat();

The concat method creates a new array that is a copy of the array with any additional items added. If you don't specify any additional items you just get a copy of the array.

Guffa
A: 

To copy an array:

var np=op.slice();

or

var np=op.concat();

concat is faster, while slice takes up one less character. Some people alias "slice" or "concat" as "copy" so that it's more obvious that you're making a copy.

No need for parameters in either case. Parameters in the slice will just make the code larger and slower.

Nosredna
According to this documentation the first parameter in slice is required. http://msdn.microsoft.com/en-us/library/26ts046k.aspx
Guffa
A: 

You can just use the native slice method which returns a copy of the array:

var np = op.slice(0,op.length);

The second parameter should be optional, but in IE I believe it is required.

Your code, complete with the change:

var op=new Array(0, 0);
var np=op.slice(0,op.length);
np[1]=2;
document.write(op+"<br>")
document.write(np)
Doug Neiner
Nope, neither parameter is needed, even for IE. The parameters just make the operation bigger and slower.
Nosredna
According to this documentation, the first parameter is required and the second is optional. http://msdn.microsoft.com/en-us/library/26ts046k.aspx
Guffa
@Guffa: neither is required in practice, in any browser.
Crescent Fresh
A: 

To create a new array you might want to consider:

   var op = [];
var op=[0,0] in this case. Object literal preferred over "new."
Nosredna