2

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"

5
  • HOW did you try to do that? Please add your attempt code. Commented Jun 17, 2015 at 6:59
  • jsfiddle.net/arunpjohny/2c0q7pwp/1 - looks fine Commented Jun 17, 2015 at 7:04
  • Why are you doing this? Why storing $scope.Address in $scope.arrayList. Can't you simply store array? Commented Jun 17, 2015 at 7:05
  • @nikhil actually i am getting the arrays dynamically but i am looping the another array into an object by specific applicationId Commented Jun 17, 2015 at 7:14
  • That is ok. What i mean is store the array corresponding to $scope.Address in a variable and then use the variable to assign the value to $scope.Address and update $scope.arrayList Commented Jun 17, 2015 at 7:21

2 Answers 2

4

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

Comments

1

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:""});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.