Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two json/javascript arrays in to one array

I have two json arrays like

var json1 = [{id:1, name: 'xxx' ...}] var json2 = [{id:2, name: 'xyz' ...}] 

I want them merge in to single arrays

var finalObj = [{id:1, name: 'xxx' ...},{id:2, name: 'xyz' ...}] 
like image 677
Murtaza Khursheed Hussain Avatar asked Apr 30 '12 13:04

Murtaza Khursheed Hussain


People also ask

How do I combine two JSON arrays?

We can merge two JSON arrays using the addAll() method (inherited from interface java.

How do I merge two JSON objects?

JSONObject to merge two JSON objects in Java. We can merge two JSON objects using the putAll() method (inherited from interface java.

How can I merge two arrays in JavaScript?

The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays. The concat() method does not change the existing arrays.

How do you combine array arrays?

To merge elements from one array to another, we must first iterate(loop) through all the array elements. In the loop, we will retrieve each element from an array and insert(using the array push() method) to another array. Now, we can call the merge() function and pass two arrays as the arguments for merging.


1 Answers

You want the concat method.

var finalObj = json1.concat(json2); 
like image 61
Quentin Avatar answered Sep 30 '22 13:09

Quentin