Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have this array:

$scope.arrayList=[{FirstName:"",LastName:""}];
$scope.Address=[{address:"",PhonNumber:""}];

and I want to push this another $scope.Address array into the first(index) object and the output should be like this:

$scope.arrayList=[{FirstName:"",LastName:"",$scope.Address}];

When I tried to push the address into the array it is creating a new object, so I tried this:

 $scope.arrayList[0].push($scope.Address);

But it's showing this error: "[0] is undefined"

share|improve this question
    
HOW did you try to do that? Please add your attempt code. – Peter Jun 17 '15 at 6:59
    
jsfiddle.net/arunpjohny/2c0q7pwp/1 - looks fine – Arun P Johny Jun 17 '15 at 7:04
    
Why are you doing this? Why storing $scope.Address in $scope.arrayList. Can't you simply store array? – nikhil Jun 17 '15 at 7:05
    
thanks @ArunPJohny – Syed Rasheed Jun 17 '15 at 7:12
    
@nikhil actually i am getting the arrays dynamically but i am looping the another array into an object by specific applicationId – Syed Rasheed Jun 17 '15 at 7:14
up vote 4 down vote accepted

I think you are looking for this

$scope.arrayList[0].Address= $scope.Address;

you can not insert array into an array of object without giving key/value pair.

Assuming $scope.Address stores an array of addresses for the $scope.arrayList[0].

If that is not the case and you want to map each array with respect to the index, then try this:

$scope.arrayList[0].Address= $scope.Address[0];
share|improve this answer

You cannot push into an object - only into an array. $scope.arrayList[0] is an object (person) not an array (address list). You have to define an array as property IN this object.

$scope.arrayList[0].addresses=[{address:"",PhonNumber:""}];

or you define the address list with the person object and use push

$scope.arrayList=[{FirstName:"",LastName:"", addresses=[]}];
$scope.arrayList[0].addresses.push({address:"",PhonNumber:""});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.