Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Jquery/Javascript中将两个json对象合并为一个json对象_Javascript_Jquery_Json - Fatal编程技术网

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

在Jquery/Javascript中将两个json对象合并为一个json对象,javascript,jquery,json,Javascript,Jquery,Json,我必须合并两个json对象,使其成为一个json对象, 我的json对象如下所示 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: Planni

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

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]
  }
};
我试过下面的代码

var merged = {};
Object.assign(merged, result, resultresult);
我也试过这个

但对我来说什么都不管用。。 请任何人帮我解决这个问题试试这个

obj_1.Template.Section = obj_1.Template.Section.concat(obj_2.Template.Section);
obj_1将保存obj_1和obj_2的段数组合并结果

试试这个

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

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

函数合并(源、目标){
Object.keys(源).forEach(函数(键){
如果(!源[键]){
返回;
}
如果(源[键]的类型=='对象'){
target[key]=target[key]| |(Array.isArray(source[key])?[]:{});
返回合并(源[key],目标[key]);
}
目标[键]=源[键];
});
}
var obj_1={?xml:{“版本”:“1.0”,“编码”:“utf-8”},“模板”:{“名称”:“资本货物工具和模具制造商L5 Set1”,“部分”:[{“Id”:“部分_1”,“名称”:“任务1:计划和协调”,“描述”:“值”:“NoofQuestions”:“0”,“IsSectionQuestionsMandatory”:“false”},{“Id”:“部分_2”,“名称”:“1号:CSC/N0307计划并协调工具和模具的制造”,“说明”:“值”:“无问题”:“0”,“IsSectionQuestionsMandatory”:“假”},空,空]},
obj_2={?xml:{“版本”:“1.0”,“编码”:“utf-8”},“模板”:{“名称”:“资本货物工具和模具制造商L5 Set1”,“章节”:[null,null,{“Id”:“章节_3”,“名称”:“任务2:按照图纸执行装配操作”,“说明”:“值”:“NoofQuestions”:“0”,“IsSectionsQuestionsMandatory”:“false”},{“Id”:第4节,“名称”:“编号2:CSC/N0308使用手动工具和手动机器对金属部件执行装配操作”,“说明”:“值”:“无问题”:“0”,“IsSectionQuestionsMandatory”:“假”},空,空]},
合并={};
合并(obj_1,合并);
合并(obj_2,合并);
console.log(合并);

.as console wrapper{max height:100%!important;top:0;}
什么不适合您,您得到的结果是什么,与您期望的结果有何不同。除此之外,这些是JavaScript对象,而不是JSON对象。JavaScript中的JSON是数据的字符串化版本。@t.niese先生,我从这个$.extend中得到的结果(true,{},x,y);不是合并,它只打印第一个变量。@MallikarJUNHAMPANVAR这是因为两个对象具有相同的属性,因此只能使用一个属性,一个对象不能有重复的属性。@chsdk是有效点,但我如何合并这两个,以任何其他方式获得此工作的结果obj_1.Template.Section=$.merge(obj_1.Template.Section,obj_2.Template.Section)这工作正常..我们能否在数组中保留空值..而不是删除them@MallikarjunHampannavar这就是我所说的,这是一个很好的实现;)实际上,合并数组中的
null
值比源数组中的值多。您可以将其填充到所需的计数。。。