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

3 Comments

[].push.apply() what you mean by [ ], first array($scope.first)?
[] 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.
[] in js represents an array. Read more here - stackoverflow.com/questions/33514915/…
0

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

Comments

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)

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.