2014-03-06 125 views
0

考虑以下两个数组。我如何将数组“行”追加到数组“array1”。我尝试过.push,但它附加在数组之外。我也尝试过。没有给我想要的结果。将javascript数组插入另一个数组

array1 = [ 
    { 
    "Activity #": "1111111", 
    "Customer": "Last, First", 
    "Tenure": "0 Year 2 Months", 
    "Account #": "0000000" 
    }]; 

lines = [ 
    { 
    "Line #": "1", 
    "Action Required": "New", 
    "Status": "Closed", 
    "Product Line": "test line1", 
    "Product": "product1" 
    }, 
    { 
    "Line #": "2", 
    "Action Required": "New", 
    "Status": "Closed", 
    "Product Line": "test line2", 
    "Product": "product2" 
    }]; 

我想要这样的事情。

my_array = [ 
{ 
    "Activity #": "1111111", 
    "Customer": "Last, First", 
    "Tenure": "0 Year 2 Months", 
    "Account #": "0000000", 
    "lines": [{ 
      "Line #": "1", 
      "fields": "keys" 
     }, 
     { 
      "Line #": "2", 
      "fields": "keys" 
     }] 
}] 

与上述使用的方法我得到了类似的东西。

my_array = [ 
{ 
    "Activity #": "1111111", 
    "Customer": "Last, First", 
    "Tenure": "0 Year 2 Months", 
    "Account #": "0000000" 
}, [ 
    { 
     "Line #": "1", 
     "fields": "keys" 
    }, { 
     "Line #": "2", 
     "fields": "keys" 
    }] 
]; 

希望有人能帮助,我的问题很清楚。

+0

@Allan:据我所知,这与JSON无关。 –

+0

是的,我确认,与json无关,编辑时发生shorcut错误。 –

回答

5

它看起来像要添加lines财产的数组内的对象,所以你必须要做到:

array1[0].lines = lines; 

您还没有解释的。如果你有更多的元素究竟会发生什么array1,所以我不能说这些。

+0

你的答案效果很好。非常感谢! – fpena06