简体   繁体   English

使用JQuery深度复制数组无法复制对象数组

[英]Deep Copying an array using JQuery cannot copy array of Objects

Following is my use case I have objects a , b which has the following structure 以下是我的用例,我有对象ab ,其结构如下

 a={arr1:[obj1={x1:1,x2:2}, obj2={x1:4,x2:4}] , arr2:[obj1={x3:55,x4:66},obj2= 
     {x3:77,x4:88}]}


 b={arr1:[obj1={x1:1,x2:2}, obj2={x1:4,x2:4},obj3={x1:44,x2:46}] , arr2:[obj1= 
     {x3:55,x4:66},obj2={x3:77,x4:88},obj1={x3:34,x4:45}]}

I want to copy the contents of a to b 我要复制的内容, ab

I tried using b=$.extend(true,b,a) 我尝试使用b=$.extend(true,b,a)

but the contents of a are not copied in b . 但内容a都不能复制b Object b remains unchanged. 对象b保持不变。 The JSON format output of b is as follows. b的JSON格式输出如下。

{"arr1":[{"x1":1,"x2":2},{"x1":4,"x2":4},{"x1":44,"x2":46}],"arr2":[{"x3":55,"x4":66},{"x3":77,"x4":88},{"x3":34,"x4":45}]}

So what is the way to copy array contents. 那么复制数组内容的方法是什么。

This way you can join a Object Array into another. 这样,您可以将一个对象数组连接到另一个数组中。

I think this is pretty straight forward. 我认为这很简单。 Questions? 有问题吗? Ask me in the comments :) 在评论中问我:)

 array1 = [{x:10, y:15}, {x:17, y:15}, {x:17, y:13}]; array2 = [{x:20, y:29}, {x:21, y:25}, {x:27, y:22}]; for(i=0; array1.length>i; i++){ array2.push(array1[i]); } console.log(array2); 

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

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