在Jquery / Javascript中将两个json对象合并为一个json对象

时间:2017-07-24 08:35:16

标签: javascript jquery json

我必须合并两个json对象并使其成为一个json对象, 我的json对象如下所示

EasyMonster

如何合并如下变量

var obj_1 = {
  "?xml": {"version": "1.0", "encoding": "utf-8"},
  "Template": {
    "Name": "Capital Goods-Tool and Die Maker L5 Set1",
    "Section": [{
      "Id": "Section_1",
      "Name": "Task 1: Planning and co-ordination",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, {
      "Id": "Section_2",
      "Name": "NOS 1: CSC/N0307 Plan and co-ordinate the making of tools and die",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, null, null]
  }
}

var obj_2 = {
  "?xml": {"version": "1.0", "encoding": "utf-8"},
  "Template": {
    "Name": "Capital Goods-Tool and Die Maker L5 Set1",
    "Section": [null, null, {
      "Id": "Section_3",
      "Name": "Task 2: Perform fitting operation as per the drawing",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, {
      "Id": "Section_4",
      "Name": "NOS 2: CSC/N0308 Perform fitting operations on metal components using hand tools and manually operated machines",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, null, null]
  }
}

我试过下面的代码

var mergedObj = {
  "?xml": {"version": "1.0", "encoding": "utf-8"},
  "Template": {
    "Name": "Capital Goods-Tool and Die Maker L5 Set1",
    "Section": [{
      "Id": "Section_1",
      "Name": "Task 1: Planning and co-ordination",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, {
      "Id": "Section_2",
      "Name": "NOS 1: CSC/N0307 Plan and co-ordinate the making of tools and die",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, {
      "Id": "Section_3",
      "Name": "Task 2: Perform fitting operation as per the drawing",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, {
      "Id": "Section_4",
      "Name": "NOS 2: CSC/N0308 Perform fitting operations on metal components using hand tools and manually operated machines",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
  }
};

我也尝试了How to merge two object values by keys

但没有什么对我有用.. 请有人帮我解决这个问题

3 个答案:

答案 0 :(得分:1)

您可以使用递归方法合并所有truthy值。



function merge(source, target) {
    Object.keys(source).forEach(function (key) {
        if (!source[key]) {
            return;
        }
        if (typeof source[key] === 'object') {
            target[key] = target[key] || (Array.isArray(source[key]) ? [] : {});
            return merge(source[key], target[key]);
        }
        target[key] = source[key];
    });
}
var obj_1 = { "?xml": { "version": "1.0", "encoding": "utf-8" }, "Template": { "Name": "Capital Goods-Tool and Die Maker L5 Set1", "Section": [{ "Id": "Section_1", "Name": "Task 1: Planning and co-ordination", "Description": "", "Value": "", "NoofQuestions": "0", "IsSectionQuestionsMandatory": "false" }, { "Id": "Section_2", "Name": "NOS 1: CSC/N0307 Plan and co-ordinate the making of tools and die", "Description": "", "Value": "", "NoofQuestions": "0", "IsSectionQuestionsMandatory": "false" }, null, null] } },
    obj_2 = { "?xml": { "version": "1.0", "encoding": "utf-8" }, "Template": { "Name": "Capital Goods-Tool and Die Maker L5 Set1", "Section": [null, null, { "Id": "Section_3", "Name": "Task 2: Perform fitting operation as per the drawing", "Description": "", "Value": "", "NoofQuestions": "0", "IsSectionQuestionsMandatory": "false" }, { "Id": "Section_4", "Name": "NOS 2: CSC/N0308 Perform fitting operations on metal components using hand tools and manually operated machines", "Description": "", "Value": "", "NoofQuestions": "0", "IsSectionQuestionsMandatory": "false" }, null, null] } },
    merged = {};

merge(obj_1, merged);
merge(obj_2, merged);

console.log(merged);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:0)

试试这个,

obj_1.Template.Section = obj_1.Template.Section.concat(obj_2.Template.Section);

obj_1将保存来自obj_1和obj_2;

的section数组的合并结果

答案 2 :(得分:0)

试试这个

obj_1.Template.Section = obj_1.Template.Section.concat(obj_2.Template.Section);
obj_1.Template.Section.sort();