23

How can I merge jquery objects together

I have

 {
  "merchantcontract":"Ready Reserve Foods 10104.01",
  "merchantcontractid":"c4253769-5a57-e111-b935-00155d010302",
  "smi_transactiondate":"\/Date(1332140400000)\/",
  "smi_glamount2":15.2600,
  "smi_transactionclass":180870001,
  "smi_transactionclassname":"Residual Agent Commission",
  "smi_contractprodcutidname":"Traditional",
  "smi_agentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5",
  "smi_primaryagentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5"
 },

 {
  "merchantcontract":"Ready Reserve Foods 10104.01",
  "merchantcontractid":"c4253769-5a57-e111-b935-00155d010302",
  "smi_transactiondate":"\/Date(1332140400000)\/",
  "smi_glamount2":2.6000,
  "smi_transactionclass":180870001,
  "smi_transactionclassname":"Residual Agent Commission",
  "smi_contractprodcutidname":"Traditional",
  "smi_agentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5",
  "smi_primaryagentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5"
     }

I would like to have

     {
      "merchantcontract":"Ready Reserve Foods 10104.01",
      "merchantcontractid":"c4253769-5a57-e111-b935-00155d010302",
      "smi_transactiondate":"\/Date(1332140400000)\/",
      "smi_glamount2":15.2600,
      "smi_transactionclass":180870001,
      "smi_transactionclassname":"Residual Agent Commission",
      "smi_contractprodcutidname":"Traditional",
      "smi_agentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5",
      "smi_primaryagentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5"
      },

       {
      "merchantcontract":"Ready Reserve Foods 10104.01",
      "merchantcontractid":"c4253769-5a57-e111-b935-00155d010302",
      "smi_transactiondate":"\/Date(1332140400000)\/",
      "smi_glamount2":2.6000,
      "smi_transactionclass":180870001,
      "smi_transactionclassname":"Residual Agent Commission",
      "smi_contractprodcutidname":"Traditional",
      "smi_agentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5",
      "smi_primaryagentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5"
       }

    {"merchantcontract":"Ready Reserve Foods 10104.01"{

      "merchantcontractid":"c4253769-5a57-e111-b935-00155d010302",
      "smi_transactiondate":"\/Date(1332140400000)\/",
      "smi_glamount2":15.2600,
      "smi_transactionclass":180870001,
      "smi_transactionclassname":"Residual Agent Commission",
      "smi_contractprodcutidname":"Traditional",
      "smi_agentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5",
      "smi_primaryagentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5"
   },
   {

      "merchantcontractid":"c4253769-5a57-e111-b935-00155d010302",
      "smi_transactiondate":"\/Date(1332140400000)\/",
      "smi_glamount2":2.6000,
      "smi_transactionclass":180870001,
      "smi_transactionclassname":"Residual Agent Commission",
      "smi_contractprodcutidname":"Traditional",
      "smi_agentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5",
      "smi_primaryagentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5"
   }
}

So both objects are under one object. I may have more than two objects.

The objects are created with each in jquery.

I am not sure how to get started on this.

Georgios Syngouroglou
  • 18,813
  • 9
  • 90
  • 92
Tom
  • 761
  • 3
  • 15
  • 33
  • Check this post http://stackoverflow.com/questions/2454295/javascript-concatenate-properties-from-multiple-objects-associative-array for possible answer. – Selvakumar Arumugam Apr 12 '12 at 20:13

5 Answers5

68

jQuery's $.extend will do what you want.

//merging two objects into new object
var new_object = $.extend({}, object1, object2);

//merge object2 into object1
$.extend(object1, object2);
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Is it possible to create a new object dynamically and add it to the parent one. What I need is all objects that have the title with a value of A grouped and all with a title of B etc.. grouped together in a new object. As it stands the json isn't grouped. There could be unlimited groups. – Tom Apr 12 '12 at 20:34
  • 1
    Yes, anything is possible! But then you are not just merging objects anymore, so you will have to iterate over the objects with $.each or a while/for loop, and add every key->value par with a certain key to one new object, and all pars with a different key to another object etc. – adeneo Apr 12 '12 at 21:46
9
anObj={'propone':'1', 'proptwo':'2'};
anotherObj={'propel':'11', 'proptlv':'12'};
var opts = {};
$.extend(opts, anObj, anotherObj, { 
    bar: "baz",
    thing: "foo"
});
console.log(opts);

Example

The Alpha
  • 143,660
  • 29
  • 287
  • 307
5

In case you wish to merge them recursively, $.extend offers the argument deep. In case deep=true the merge become recursively. An example above,

// Initialize two objects.
var json1 = { "a": { "a1": "value1", "a2": "value2" }};
var json2 = { "a": { "a3": "value3" }};

// Merge them recursively.
var newJson = $.extend(true, {}, json1, json2);

// Test it.
if (newJson.a.a1 && newJson.a.a3) {
    console.log("Success");
}
Georgios Syngouroglou
  • 18,813
  • 9
  • 90
  • 92
5

If you want to update your existing object, use:

$.extend(obj1, obj2); //update obj1 and put all obj2 values in it.

$.extend will change the values of your existing object

To create a new third object that is the sum of both objects, you can use this function

function merger_objects(obj1,obj2){
  $.each(obj2, function(key, value) {
     obj1[key] = value;    
  });
 return obj1;
}

Example

var fruit = {apple:"sweet",graps:"bitter"}
var vege = {cucamber:"bitter",letuce:"bitter"}

var fruit_and_vege = merger_objects(fruit,vege); 
//returns {apple:"sweet",graps:"bitter",cucamber:"bitter",letuce:"bitter"}
Kareem
  • 5,068
  • 44
  • 38
0

can you put them in an array?

var myObjects = [];

and in your each where they are created just add:

myObjects.push(newObject);
Evan
  • 5,975
  • 8
  • 34
  • 63