0

I have two arrays now once i rendered the data from backend i want to push just object from array2 to array1 not the array itself.

How can i just push object from array2 to array1 I dont want to push as array.

ctrl.js

var array1 = [{name:'john', address:'cliffwood ave'}]

var array2 = [{name:'Mike', address:'florence ave'}]

array1.push(array2);

5 Answers 5

2

If you want to mutate array1:

array1.push.apply(array1, array2);

Otherwise:

var array3 = array1.concat(array2);
Sign up to request clarification or add additional context in comments.

Comments

1

If you wanted to push a single object in the array, you could just reference it by it's specific index :

array1.push(array2[0]);

Otherwise, if you wanted to push all of the items, you might consider just concatenating them via the concat() function :

array1.concat(array2);

Comments

1

If you want to use ES6 you can use the spread operator:

array1.push(...array2);

which is functionally equivalent to this ES5 method...

 array1.push.apply(array1, array2);

...mentioned in one of the other answers.

DEMO

Comments

0

To push {name:'Mike', address:'florence ave'} into array1 :

array1.push(array2[0]);

As the object you want is simply the first element in your array2 variable.

Comments

0
(var I = 0; I <= array2.length-1 ; I++){
  array1.push(array2[I])
}

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.