复制对象的数组在JavaScript另一个数组 [英] Copying an array of objects into another array in javascript

查看:295
本文介绍了复制对象的数组在JavaScript另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何可以复制一个数组(其中的元素是对象)的每一个元素,到另一个数组,以使它们完全独立?我不想在一个阵列中改变一个元素影响其他。


解决方案

如果目标阵列还不存在,你可以使用片() CONCAT(); 片()(或片(0),但参数默认为 0 )可能是更地道:

  VAR destinationArray = sourceArray.slice(); //也许更地道
// 要么
变种destinationArray = sourceArray.concat();

由于ES2015(又名ES6)的,还有还有 Array.from ,它创建任何阵列状的东西(包括实际的数组)的新数组:

  VAR destinationArray = Array.from(sourceArray);

Array.from 可匀/ polyfilled老年JavaScript引擎。)

在此之后,这两个阵列将具有相同的内容。改变一个数组不会改变对方。当然,如果一个数组项是一个对象,对于两个阵列该对象将指向同一对象的条目;这不是一个深的副本。

如果目标数组存在,您希望将源数组的内容追加到它,你可以使用

  destinationArray.push.apply(destinationArray,sourceArray);

这是通过调用目标阵列上的工作适用的JavaScript函数功能,该功能可让您指定参数的函数调用数组。 将推动尽可能多的元素,因为它有参数,所以它最终从源数组中的元素复制到目标数组。

在ES2015以后,你可以用在S $ P $垫操作员清洁( ...

  destinationArray.push(... sourceArray);

下面是一个ES5版本:

\r
\r

VAR来源1,dest1,源2,dest2;\r
\r
snippet.log(如果dest尚不存在:);\r
source1中= [1,2,3,4〕;\r
dest1 = source1.slice(0);\r
snippet.log([前更改] source1中的=+ source1.join());\r
snippet.log([前更改] dest1 =+ dest1.join());\r
来源1 [2] =三包;\r
dest1 [3] =四;\r
snippet.log([变更以后] source1中的=+ source1.join());\r
snippet.log([变更以后] dest1 =+ dest1.join());\r
\r
snippet.log(如果dest已经存在,我们只是追加:);\r
源2 = [1,2,3,4〕;\r
dest2 = ['一','B','C','D'];\r
snippet.log([前追加]源2 =+ source2.join());\r
snippet.log([前追加] dest2 =+ dest2.join());\r
dest2.push.apply(dest2,源2);\r
snippet.log([前更改]源2 =+ source2.join());\r
snippet.log([前更改] dest2 =+ dest2.join());\r
源2 [2] =三包;\r
dest2 [7] =四;\r
snippet.log([变更以后]源2 =+ source2.join());\r
snippet.log([变更以后] dest2 =+ dest2.join());

\r

< - 脚本提供了`snippet`对象,请参见http:/ /meta.stackexchange.com/a/242144/134069 - >\r
&LT;脚本的src =// tjcrowder.github.io/simple-snippets-console/snippet.js\"></script>

\r

\r
\r

How can I copy every element of an array (where the elements are objects), into another array, so that they are totally independent? I don't want changing an element in one array to affect the other.

解决方案

If the destination array doesn't exist yet, you can use slice() or concat(); slice() (or slice(0), but the argument defaults to 0) is probably more idiomatic:

var destinationArray = sourceArray.slice(); // Probably more idiomatic
// or
var destinationArray = sourceArray.concat();

As of ES2015 (aka ES6), there's also Array.from, which creates a new array from any array-like thing (including an actual array):

var destinationArray = Array.from(sourceArray);

(Array.from can be shimmed/polyfilled for older JavaScript engines.)

After that, both arrays will have the same contents. Changing one array will not change the other. Naturally, if an array entry is an object, the entry for that object in both arrays will point to the same object; this isn't a "deep" copy.

If the destination array exists and you want to append the contents of the source array to it, you can use push:

destinationArray.push.apply(destinationArray, sourceArray);

That works by calling push on the destination array using the apply feature of JavaScript functions, which lets you specify the arguments for the function call as an array. push will push as many elements as it has arguments, so it ends up copying the elements from the source array to the destination array.

In ES2015 and later, you can make that cleaner with the spread operator (...):

destinationArray.push(...sourceArray);

Here's an ES5 version:

var source1, dest1, source2, dest2;

snippet.log("If dest doesn't exist yet:");
source1 = [1, 2, 3, 4];
dest1 = source1.slice(0);
snippet.log("[before change] source1 = " + source1.join(", "));
snippet.log("[before change] dest1 = " + dest1.join(", "));
source1[2] = "three";
dest1[3] = "four";
snippet.log("[after change] source1 = " + source1.join(", "));
snippet.log("[after change] dest1 = " + dest1.join(", "));

snippet.log("If dest already exists and we're just appending:");
source2 = [1, 2, 3, 4];
dest2 = ['a', 'b', 'c', 'd'];
snippet.log("[before append] source2 = " + source2.join(", "));
snippet.log("[before append] dest2 = " + dest2.join(", "));
dest2.push.apply(dest2, source2);
snippet.log("[before change] source2 = " + source2.join(", "));
snippet.log("[before change] dest2 = " + dest2.join(", "));
source2[2] = "three";
dest2[7] = "four";
snippet.log("[after change] source2 = " + source2.join(", "));
snippet.log("[after change] dest2 = " + dest2.join(", "));

<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

这篇关于复制对象的数组在JavaScript另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆