简体   繁体   English

如何在JavaScript中保留数组?

[英]How to retain array in javascript?

Here's some code that has two arrays(np and op), one a copy of the other 这是一些具有两个数组(np和op)的代码,一个数组是另一个数组的副本

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? 有什么办法可以保留原件并修改副本?

Some of the built in Array functions will actually create a copy for you. 一些内置的Array函数实际上会为您创建一个副本。 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 参考http://my.opera.com/GreyWyvern/blog/show.dml/1725165

You never made a copy of the array. 您从未复制过阵列。 You simply assigned the array to another variable. 您只需将数组分配给另一个变量。 This does not copy in Javascript. 这不会用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. 没有使用Javascript复制数组的内置方法,因此您将需要编写自己的函数来执行此操作。

Take a look at this StackOverflow question for a hint on how to actually implement the copying of the elements in the array. 看一下这个StackOverflow问题 ,以获取有关如何实际实现数组中元素复制的提示。

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. concat方法创建一个新数组,该数组是添加了任何其他项的该数组的副本。 If you don't specify any additional items you just get a copy of the array. 如果您不指定任何其他项目,则只需获取阵列的副本。

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)

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. 有几种方法可以做到这一点,例如, 这里是使用jQuery克隆对象的方法。

To copy an array: 复制数组:

var np=op.slice();

or 要么

var np=op.concat();

concat is faster, while slice takes up one less character. concat更快,而slice少占用一个字符。 Some people alias "slice" or "concat" as "copy" so that it's more obvious that you're making a copy. 有些人将“ slice”或“ concat”别名为“ copy”,以便更明显地表明您正在复制。

No need for parameters in either case. 两种情况下都不需要参数。 Parameters in the slice will just make the code larger and slower. 切片中的参数只会使代码变大和变慢。

You can just use the native slice method which returns a copy of the array: 您可以只使用本机slice方法,该方法返回数组的副本:

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

The second parameter should be optional, but in IE I believe it is required. 第二个参数应该是可选的,但是在IE中,我认为它是必需的。

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)

要创建一个新数组,您可能需要考虑:

   var op = [];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM