6

I created the following array:

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

I tried

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

where results.data looks something like this:

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

However this does not seem to work as it tries to add an array as the second element. What I need is to have the contents of the array result.data appended to the array $scope.testAccounts

Please note that all the examples I have seen so far seem to not work if the array is an array of objects. This is what I have. Thanks

1 Answers1

4

You're looking for Array.concat

> foo = [1,2,3]
[1, 2, 3]
> foo.concat([4,5,6])
[1, 2, 3, 4, 5, 6]
georg
  • 195,833
  • 46
  • 263
  • 351
  • The problem is that my 1,2,3,4,5,6 are objects such as {id: 0, name:"x"}. I can't get it to work when they are objects. –  May 03 '13 at 07:08
  • @Anne: array functions generally don't care what arguments you provide. `concat` should work just fine with objects. Could you post your exact data and code? – georg May 03 '13 at 07:12
  • Thanks for your help. I tried again and it worked. –  May 03 '13 at 07:17