Reputation: 315
I have to merge two json object and make it one json object, My json objects are like below
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]
}
}
How do I merge like below variable
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]
}
};
I have tried below code
var merged = {};
Object.assign(merged, result, resultresult);
and also I have tried this also How to merge two object values by keys
but nothing is working for me.. Please anybody help me to resolve this
Upvotes: 1
Views: 1457
Reputation: 386578
You could merge all truthy values with a recursive approach.
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; }
Upvotes: 1
Reputation: 135
Try this
obj_1.Template.Section = obj_1.Template.Section.concat(obj_2.Template.Section);
obj_1.Template.Section.sort();
Upvotes: 0
Reputation: 997
Try this,
obj_1.Template.Section = obj_1.Template.Section.concat(obj_2.Template.Section);
obj_1 will hold the merged result of section array from obj_1 and obj_2;
Upvotes: 0