1

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?

1
  • 3
    Instead of "var op=new Array(0,0)", "var op=[0,0]" is preferred. Commented Nov 28, 2009 at 17:08

8 Answers 8

7

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

Sign up to request clarification or add additional context in comments.

Comments

5

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.

3 Comments

Not absolutely necessary, if using an Array see my simpler example below. For an Object you will need to use the solution referenced.
The question you link to does not provide a solution for copying arrays.
@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.
4

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.

Comments

2
Array.prototype.copy = function() {
    return this.slice(0, this.length);
  }

Then


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

Comments

1

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.

2 Comments

Oh come on. That's like saying "you should do the right thing" without saying what the right thing is.
I don't even understand this answer. @Igor, could you clarify?
0

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)

3 Comments

Nope, neither parameter is needed, even for IE. The parameters just make the operation bigger and slower.
According to this documentation, the first parameter is required and the second is optional. msdn.microsoft.com/en-us/library/26ts046k.aspx
@Guffa: neither is required in practice, in any browser.
0

To create a new array you might want to consider:

   var op = [];

1 Comment

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

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.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.