7

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

8
  • 3
    stackoverflow.com/questions/351409/appending-to-array Commented May 3, 2013 at 6:53
  • Check your result.data if it matches your array layout. Commented May 3, 2013 at 6:53
  • 1
    Check this: stackoverflow.com/questions/1374126/… and stackoverflow.com/questions/351409/appending-to-array?rq=1 Commented May 3, 2013 at 6:54
  • The problem is my array comprises of an object in the first row followed by another array in the second. What I need to do is to add the contents of the second array to the first. What happens is my code adds the whole array as the second element. Commented May 3, 2013 at 6:57
  • Sorry. I just above provided more details Commented May 3, 2013 at 6:59

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

3 Comments

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.
@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?
Thanks for your help. I tried again and it worked.