I have control over the JSON.
I'm willing to either recursively loop through the objects, or use regex to merge the strings and then eval...either way.
But I've spent all week on this damn thing and I can't do it anymore.
I'm almost there, but each solution is not quite good enough.
So, for example in the code below, the first object has an Address object with several properties, and the second object has that same Address object with different properties.
Currently, the properties of the first object are getting overriden, instead of merged.
If the properties are the same in both objects, then they can be overridden (no duplicates), but if they're different, then I'd like them to be appended...is this simple ??
The following code "almost" does what I want, but it just overwrites after the first level, so I think it needs recursion.
I'm very open to your string idea at this point.
- var firstObj = { "Type":"1", "Company": {"CompanyAddresses":[ {"Address": {"PostalCode":"V6H1S2", "AddressLine1":"3318 Main"}}]} };
-
var secondObj = { "Type":"1", "Company": {"CompanyAddresses":[ {"Address": {"AddressLine2":"#2"}}]} }
- for(var i in objA ) {
-
var found = new Array();
-
for(var j in objB ) {
-
if(i==j) {
-
found.push(j);
-
for(var k in objB[j] ) {
-
objA[i][k] = objB[j][k];
-
}
-
}
-
}
-
for(var j in objB) {
-
if(found.indexOf(j) == -1) {
-
objA[j] = objB[j];
-
}
-
}
-
}