Catch only what was changed in an object

2

I have implemented a PATCH function to update the data of a resource, but I need to get only the updated data of an object already loaded on the screen to send in the request.

What would be the most efficient way to do this? Can you use $ watch or is it best to avoid?

    
asked by anonymous 13.02.2017 / 12:05

1 answer

1

It was much simpler than I thought. I just created a function that compares two objects and returns an object with the changes.

function diffBetween(old, cur) {
    var updated = {};
    for (var prop in cur) {
        if (old[prop] != cur[prop]) {
            updated[prop] = cur[prop];
        }
    }
    return updated;
}
    
17.02.2017 / 20:01