Merge two object arrays in javascript

Question:

I wanted to know how I can merge two arrays of objects * (putting one inside the other would also work for me). *

For example:

array["nombre": "paco", "edad": "21"];
array2["nombre": "matias", "edad": "25"];

it would look like this:

arraydefinitivo[array, array2]

I've tried push , but it doesn't work for me for this.

Answer:

You can use concant to join 2 arrays

 var array1=[1,2,3]; var array2=[4,5,6]; array1=array1.concat(array2); console.log(array1);

Other alternative [].concat(array1, array2, array3);

var array1 = [1, 2, 3];
var array2 = [4, 5, 6];
var array3 = [7, 8, 9];
array1 = [].concat(array1, array2, array3);
console.log(array1);

Now if you want to concatenate discriminating by some condition, for example only concatenate the even numbers of 2 arrays

var array1 = [1, 2, 3];
var array2 = [4, 5, 6];

array2.filter(data => data%2==0 ? array1.push(data):data);
console.log(array1);
Scroll to Top