2

I want to generate an object from two given objects A and B with only the values from B that differ from A. We can assume that the all fields exist in both A and B.

Example: Given the following two objects:

A

{
    "firstName": "John",
    "lastName": "Doe",
    "nickname": "Johnny",
    "location": {
        "latitude": 1.0,
        "longitude": 1.0
    },
    "email": "[email protected]"
}

B

{
    "firstName": "John",
    "lastName": "Doe",
    "nickname": "John-Boy",
    "location": {
        "latitude": 1.0,
        "longitude": 2.0
    },
    "email": "[email protected]"
}

As nickname and location have changed, I want the result to be:

{
    "nickname": "John-Boy",
    "location": {
        "latitude": 1.0,
        "longitude": 2.0
    }
}

Note that I want the full location object and not just the changed longitude

What would be a good way of achieving this?

2

2 Answers 2

1

var array1 = {
    "firstName": "John",
    "lastName": "Doe",
    "nickname": "Johnny",
    "location": {
        "latitude": 1.0,
        "longitude": 1.0
    },
    "email": "[email protected]"
}

var array2 = {
    "firstName": "John",
    "lastName": "Doe",
    "nickname": "John-Boy",
    "location": {
        "latitude": 1.0,
        "longitude": 2.0
    },
    "email": "[email protected]"
}

var currentString;
		var resultObject = {};
	function loopOverObject(sourceObj, targetObj,innerObjName) {
		for (var key in sourceObj) {
			currentString = null;
			if (sourceObj.hasOwnProperty(key)) {
				
				if (typeof sourceObj[key] === "object") {
					loopOverObject(sourceObj[key],targetObj,key);
					break;
				}
				else {
					if (sourceObj[key] !== targetObj[key]) {
						if (innerObjName) {
							if (resultObject[innerObjName]) {
								resultObject[innerObjName][key] = sourceObj[key];
							}
							else {
								resultObject[innerObjName] = JSON.parse('{"'+key+'": '+sourceObj[key]+'}');
							}
						}
						else {
							currentString = sourceObj[key];
						}
					}
				}
				if (currentString) {
					resultObject[key]=currentString;
				}
			}
		}
		return resultObject;
	};
	var result = loopOverObject(array2,array1);
	
var resultDiv = document.getElementById("result")
resultDiv.innerHTML = JSON.stringify(result);
<div id="result"></div>

Sign up to request clarification or add additional context in comments.

Comments

0

You could use a differencing module like odiff by getting the list of differences and using them to build a new object appropriately:

var diff = odiff(A,B)
var result= {}
for(var n=0; n<diff.length; n++) {
  var key = diff.path[0] // the top-level key
  result[key] = B[key]
} 

Then you got the result I think you want - that is if you only care about getting the top-level fields that have changed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.