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.
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.
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.
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.
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.
anObj={'propone':'1', 'proptwo':'2'};
anotherObj={'propel':'11', 'proptlv':'12'};
var opts = {};
$.extend(opts, anObj, anotherObj, {
bar: "baz",
thing: "foo"
});
console.log(opts);
Example
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With