Joining 2 JSON objects with nested objects
Joining 2 JSON objects with nested objects
by Andrew Pougher
JavaScript
var x = { "feeds": [
{
"name": "Full Feed",
"id": "13",
"checked": true
},
{
"name": "Trip Feed",
"id": "53",
"checked": false
},
{
"name": "Flight Feed",
"id": "27",
"checked": true
}
]};
var y = {"emailalerts": [
{
"alertText": "When a hotel is cancelled",
"alertName": "cxlHotel",
"savedValue": true
},
{
"alertText": "When a car is cancelled",
"alertName": "cxlCar",
"savedValue": false
},
{
"alertText": "When a hotel Segment is cancelled",
"alertName": "cxlHotelSeg",
"savedValue": false
},
{
"alertText": "When a Car Segment is cancelled",
"alertName": "cxlCarSeg",
"savedValue": false
},
{
"alertText": "Return Flight Reminders",
"alertName": "cxlFltRem",
"savedValue": false
},
{
"alertText": "Check In Reminders",
"alertName": "cxlChkRem",
"savedValue": true
}
]};
function extend(target) {
var sources = [].slice.call(arguments, 1);
sources.forEach(function (source) {
for (var prop in source) {
target[prop] = source[prop];
}
});
return target;
}
console.log (JSON.stringify(extend({}, x, y )));
var myArrayOfObjects = "[" + JSON.stringify(extend({}, x, y )) + "]";
$("code").html(myArrayOfObjects)