gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:20:09 25 4
gpt4 key购买 nike

我必须合并两个 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);

我也试过这个How to merge two object values by keys

但对我来说没有任何用处..请任何人帮我解决这个问题

最佳答案

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

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

关于javascript - 在 Jquery/Javascript 中将两个 json 对象合并为一个 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45275809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com