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?

pop850
  • 3,157
  • 3
  • 29
  • 34

8 Answers8

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

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
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.

Community
  • 1
  • 1
Marc W
  • 19,083
  • 4
  • 59
  • 71
  • Not absolutely necessary, if using an Array see my simpler example below. For an Object you will need to use the solution referenced. – David Snabel-Caunt Nov 28 '09 at 17:04
  • 1
    The question you link to does not provide a solution for copying arrays. – James Nov 28 '09 at 17:05
  • @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 Nov 28 '09 at 17:34
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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
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)
simon
  • 12,666
  • 26
  • 78
  • 113
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.

Community
  • 1
  • 1
Igor Pavelek
  • 1,444
  • 14
  • 22
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.

Nosredna
  • 83,000
  • 15
  • 95
  • 122
  • According to this documentation the first parameter in slice is required. http://msdn.microsoft.com/en-us/library/26ts046k.aspx – Guffa Nov 28 '09 at 23:43
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)
Doug Neiner
  • 65,509
  • 13
  • 109
  • 118
  • 2
    Nope, neither parameter is needed, even for IE. The parameters just make the operation bigger and slower. – Nosredna Nov 28 '09 at 17:14
  • According to this documentation, the first parameter is required and the second is optional. http://msdn.microsoft.com/en-us/library/26ts046k.aspx – Guffa Nov 28 '09 at 23:44
  • 1
    @Guffa: neither is required in practice, in any browser. – Crescent Fresh Nov 29 '09 at 01:47
0

To create a new array you might want to consider:

   var op = [];
Dana Benson
  • 966
  • 7
  • 16