![](/img/trans.png)
[英]Copying an array of objects into another array in javascript (Deep Copy)
[英]Deep Copying an array using JQuery cannot copy array of Objects
以下是我的用例,我有對象a
, b
,其結構如下
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}]}
我要復制的內容, a
以b
我嘗試使用b=$.extend(true,b,a)
但內容a
都不能復制b
。 對象b
保持不變。 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}]}
那么復制數組內容的方法是什么。
這樣,您可以將一個對象數組連接到另一個數組中。
我認為這很簡單。 有問題嗎? 在評論中問我:)
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.