om_jaipur
om_jaipur

Reputation: 2196

How to push all objects into another array using AngularJs

I have 2 arrays,

$scope.first = [
  { fName:'Alex', lName='Doe' },
  { fName:'John', lName='S' }
]

var second= [
  { fName:'Tom', lName='M', email:'[email protected]' },
  { fName:'Jerry', lName='L', email:'[email protected]' }
]

I need to push second array into first array and want to result like:

$scope.first = [
  { fName:'Alex', lName='Doe' },
  { fName:'John', lName='S' },
  { fName:'Tom', lName='M', email:'[email protected]' },
  { fName:'Jerry', lName='L', email:'[email protected]' }
]

Upvotes: 0

Views: 4257

Answers (3)

user7110739
user7110739

Reputation:

$scope.first = [
  { fName:'Alex', lName='Doe' },
  { fName:'John', lName='S' }
]

var second= [
  { fName:'Tom', lName='M', email:'[email protected]' },
  { fName:'Jerry', lName='L', email:'[email protected]' }
]

$scope.first = $scope.first.concat(second)

Upvotes: 0

Lehlohonolo
Lehlohonolo

Reputation: 126

I would try $scope.first.concat($scope.second)

Upvotes: 0

hansmaad
hansmaad

Reputation: 18905

If you want to push elements from one array into an existing array you can do

[].push.apply($scope.first, second);

If you want to create a new array that contains elements of both arrays, use concat:

$scope.first = $scope.first.concat(second);

Upvotes: 2

Related Questions