2014-03-06 48 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