简体   繁体   中英

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:'tom@gmail.com' },
  { fName:'Jerry', lName='L', email:'jerry@gmail.com' }
]

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:'tom@gmail.com' },
  { fName:'Jerry', lName='L', email:'jerry@gmail.com' }
]

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

我会尝试$ scope.first.concat($ scope.second)

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

var second= [
  { fName:'Tom', lName='M', email:'tom@gmail.com' },
  { fName:'Jerry', lName='L', email:'jerry@gmail.com' }
]

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

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