jsondiffpatch icon indicating copy to clipboard operation
jsondiffpatch copied to clipboard

Get object of changed properties only

Open OliverJAsh opened this issue 9 years ago • 3 comments

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?

OliverJAsh avatar Feb 04 '16 17:02 OliverJAsh

For context, I want to use the difference to apply a patch to another kind of object.

OliverJAsh avatar Feb 04 '16 18:02 OliverJAsh

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.

benjamine avatar Feb 04 '16 18:02 benjamine

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;
    }
}

PeterStAngelo avatar Feb 28 '16 02:02 PeterStAngelo