Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery merge two objects

Tags:

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.

like image 823
Tom Avatar asked Apr 12 '12 20:04

Tom


People also ask

How do you join two objects?

If you want to merge the second object into the first object, instead of creating a new object, you can use Object. assign() . The Object. assign(target, source) function merges the source into the target.

How do you join two objects in JavaScript?

To merge objects into a new one that has all properties of the merged objects, you have two options: Use a spread operator ( ... ) Use the Object. assign() method.

How do you clone an object in jQuery?

To clone an element using jQuery, use the jQuery. clone() method. The clone() method clones matched DOM Elements and select the clones. This is useful for moving copies of the elements to another location in the DOM.

How do I merge two objects in TypeScript?

Use the spread syntax (...) to merge objects in TypeScript, e.g. const obj3 = { ... obj1, ... obj2 } . The type of the final object will successfully be inferred, so trying to add or remove properties from it will cause the type checker to show an error.


2 Answers

anObj={'propone':'1', 'proptwo':'2'};
anotherObj={'propel':'11', 'proptlv':'12'};
var opts = {};
$.extend(opts, anObj, anotherObj, { 
    bar: "baz",
    thing: "foo"
});
console.log(opts);

Example

like image 22
The Alpha Avatar answered Oct 02 '22 07:10

The Alpha


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);
like image 125
adeneo Avatar answered Oct 02 '22 07:10

adeneo