24

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.

1

5 Answers 5

72

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
10
anObj={'propone':'1', 'proptwo':'2'};
anotherObj={'propel':'11', 'proptlv':'12'};
var opts = {};
$.extend(opts, anObj, anotherObj, { 
    bar: "baz",
    thing: "foo"
});
console.log(opts);

Example

Comments

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");
}

Comments

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"}

Comments

0

can you put them in an array?

var myObjects = [];

and in your each where they are created just add:

myObjects.push(newObject);

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.