考虑以下两个数组.如何将数组"行"附加到数组"array1".我试过.push但是它附加在数组之外.我也试过.unshift,它没有给我想要的结果.
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"
}];
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西.
my_array = [
{
"Activity #": "1111111",
"Customer": "Last, First",
"Tenure": "0 Year 2 Months",
"Account #": "0000000",
"lines": [{
"Line #": "1",
"fields": "keys"
},
{
"Line #": "2",
"fields": "keys"
}]
}]
Run Code Online (Sandbox Code Playgroud)
通过上面提到的使用方法我得到了类似的东西
my_array = [
{
"Activity #": "1111111",
"Customer": "Last, First",
"Tenure": "0 Year 2 Months",
"Account #": "0000000"
}, [
{
"Line #": "1",
"fields": "keys"
}, {
"Line #": "2",
"fields": "keys"
}]
];
Run Code Online (Sandbox Code Playgroud)
希望有人可以提供帮助,我的问题很明确.
看起来你想要lines在数组中添加属性到对象,所以你必须这样做:
array1[0].lines = lines;
Run Code Online (Sandbox Code Playgroud)
如果你有更多的元素array1,你还没有解释究竟应该发生什么,所以我不能说什么.