Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join two JSON Array objects in Node

How to join two JSON Array objects in Node.

I want to join obj1 + obj2 so I can get the new JSON object:

obj1 = [ { t: 1, d: 'AAA', v: 'yes' },
         { t: 2, d: 'BBB', v: 'yes' }]

obj2 = [ { t: 3, d: 'CCC', v: 'yes' },
        { t: 4, d: 'DDD', v: 'yes' }]


output = [ { t: 1, d: 'AAA', v: 'yes' },
           { t: 2, d: 'BBB', v: 'yes' },
           { t: 3, d: 'CCC', v: 'yes' },
           { t: 4, d: 'DDD', v: 'yes' }]
like image 510
coriano35 Avatar asked Dec 20 '16 03:12

coriano35


People also ask

How do I combine 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 do you combine two objects in an array?

Using the spread operator or the concat() method is the most optimal solution. If you are sure that all inputs to merge are arrays, use spread operator . In case you are unsure, use the concat() method. You can use the push() method to merge arrays when you want to change one of the input arrays to merge.

How do I combine two Jsonarrays?

simple. JSONArray class to merge two JSON arrays in Java. We can merge two JSON arrays using the addAll() method (inherited from interface java.


3 Answers

var output = obj1.concat(obj2);

like image 159
therobinkim Avatar answered Oct 16 '22 18:10

therobinkim


obj1 = [ { t: 1, d: 'AAA', v: 'yes' },
         { t: 2, d: 'BBB', v: 'yes' }]

obj2 = [ { t: 3, d: 'CCC', v: 'yes' },
        { t: 4, d: 'DDD', v: 'yes' }]

var output = obj1.concat(obj2);

console.log(output);
like image 38
Vaghani Janak Avatar answered Oct 16 '22 17:10

Vaghani Janak


try

  Object.assign(obj1, obj2);

For Details check Here

 var o1 = { a: 1 };
 var o2 = { b: 2 };
 var o3 = { c: 3 };

 var obj = Object.assign(o1, o2, o3);
 console.log(obj); // { a: 1, b: 2, c: 3 }
like image 25
User Avatar answered Oct 16 '22 16:10

User