简体   繁体   中英

How to push an array into an array of objects using AngularJs

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"

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];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM