jquery合并两个对象

时间:2012-04-12 20:01:53

标签: javascript jquery

如何将jquery对象合并在一起

我有

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

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

因此两个对象都在一个对象下。我可能有两个以上的对象。

在jquery中使用每个对象创建对象。

我不知道如何开始这个。

5 个答案:

答案 0 :(得分:57)

jQuery的$.extend会做你想做的事。

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

//merge object2 into object1
$.extend(object1, object2);

答案 1 :(得分:8)

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

Example

答案 2 :(得分:3)

如果您希望以递归方式合并它们,$.extend会提供参数deep。如果deep=true合并成为递归。上面的一个例子,

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

答案 3 :(得分:0)

你能把它们放在一个数组中吗?

var myObjects = [];

并在每个创建它们的位置添加:

myObjects.push(newObject);

答案 4 :(得分:0)

如果要更新现有对象,请使用:

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

$。extend将更改您现有对象的值

要创建一个新的第三个对象即两个对象的总和,可以使用此功能

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

示例

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