0

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]' } ] 
1
  • 3
    with concat...?CommentedAug 25, 2017 at 12:08

3 Answers 3

2

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); 
3
  • 1
    [].push.apply() what you mean by [ ], first array($scope.first)?
    – om_jaipur
    CommentedAug 25, 2017 at 12:19
  • [] is an array :) You could also write Array.prototype.push.apply(...) or $scope.first.push.apply(...) or second.push.apply(...). It doesn't matter. It#s just a way to reference the push method.
    – hansmaad
    CommentedAug 25, 2017 at 12:21
  • [] in js represents an array. Read more here - stackoverflow.com/questions/33514915/…
    – abhig10
    CommentedAug 25, 2017 at 12:23
0

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

    0
    $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) 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.