I have a dilemma involving two JavaScript objects
var order1 = {
sandwich: 'tuna',
chips: true,
drink: 'soda',
order: 1,
toppings: [{VendorNumber: 18, PreferredFlag: false, SupportedFlag: true}, {VendorNumber: 19, PreferredFlag: false, SupportedFlag: true}, {VendorNumber: 20, PreferredFlag: false, SupportedFlag: true}],
details: {
name: 'Chris',
phone: '555-555-5555',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f31301f2b373e313426302a713c3032">[email protected]</a>'
},
otherVal1: '1'
};
var order2 = {
sandwich: 'turkey',
chips: true,
drink: 'soda',
order: 2,
toppings: [{VendorNumber: 18, PreferredFlag: false, SupportedFlag: true}, {VendorNumber: 19, PreferredFlag: false, SupportedFlag: false}, {VendorNumber: 20, PreferredFlag: true, SupportedFlag: true}],
details: {
name: 'Jon',
phone: '(555) 555-5555',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="38415d4b7848545d594b5d165b5755">[email protected]</a>'
},
otherVal1: '2'
};
The challenge is to compare these two objects (order1 being the original and order2 as the edited data), and store the differences in a new variable named var order3. It is important that when there is an array inside an object like the toppings array, it should be copied as a whole with any changes included.
In essence, the desired result would look like this:
{
details: {
email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="265f435566564a434755430845494b">[email protected]</a>",
name: "Jon",
phone: "(555) 555-5555"
},
order: 2,
otherVal1: "2",
sandwich: "turkey",
toppings: [{
PreferredFlag: false,
SupportedFlag: true,
VendorNumber: 18
}, {
PreferredFlag: false,
SupportedFlag: false,
VendorNumber: 19
}, {
PreferredFlag: true,
SupportedFlag: true,
VendorNumber: 20
}]
}
How can I accomplish this task?