如何将对象数组附加到现有数组?

时间:2013-05-03 06:51:21

标签: javascript

我创建了以下数组:

$scope.testAccounts[0] = { id: 99, name: "Select Account" };

我试过

$scope.testAccounts.push(result.data);

其中results.data看起来像这样:

[{ id: 1, name: "x" },{ id: 2, name: "y" }]

然而,这似乎不起作用,因为它试图添加一个数组作为第二个元素。我需要的是将数组result.data的内容附加到数组$ scope.testAccounts

请注意,如果数组是一个对象数组,到目前为止我看到的所有示例似乎都不起作用。这就是我所拥有的。感谢

1 个答案:

答案 0 :(得分:4)

您正在寻找Array.concat

> foo = [1,2,3]
[1, 2, 3]
> foo.concat([4,5,6])
[1, 2, 3, 4, 5, 6]
相关问题