Get object of changed properties only
Given a
{
"style": {},
"innerHTML": ""
}
and b
{
"style": {},
"innerHTML": "foo"
}
I want an object that represents only the changes between the two, a and b:
{
"innerHTML": "foo"
}
Is this possible or do I need to write something that traverses the diff?
For context, I want to use the difference to apply a patch to another kind of object.
hi, you want to apply it with a sort of deep merge (alla lodash merge)?
to obtain that type of delta yes, you'd have to traverse it, and whenever you find an array, take either the 2nd value ([oldValue, newValue], a replace), or the single value if array length is 1 ([newValue], an insert).
the problem I see is that won't handle deletes ([oldValue, 0, 0]), or other things like array moves.
for patching another object can't you use just jsondiffpatch.patch(obj, delta)?, it seems to me it would give you the same result out-of-the-box, but handling deletes too.
Hi, I have the following code (which works) -but is there anything else I should consider (from the jsondiffpatch.diff delta output)?
//var delta = jsondiffpatch.diff(original, edited);
var delta = {
"Name": ["PSA 123", "PSA 999"],
"Email": ["[email protected]", "[email protected]"],
"Note": 123456
};
var newDelta = {};
for (var item in delta) {
if (delta.hasOwnProperty(item)) {
var value = delta[item];
newDelta[item] = Array.isArray(value) ? value[1] : value;
}
}